Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql@^0.8.x compat #5

Merged
merged 6 commits into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# graphql-parse-fields [![Build Status](https://travis-ci.org/tjmehta/graphql-parse-fields.svg)](https://travis-ci.org/tjmehta/graphql-parse-fields) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
# graphql-parse-fields [![Build Status](https://travis-ci.org/tjmehta/graphql-parse-fields.svg)](https://travis-ci.org/tjmehta/graphql-parse-fields?branch=master) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
Parse fields from AST (GraphQLResolveInfo) into a JSON tree

## Installation
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ function parseFields (/* dynamic */) {
var tree
var info = arguments[0]
var keepRoot = arguments[1]
if (info.fieldASTs) {
var fieldNodes = info && (info.fieldASTs || info.fieldNodes)
if (fieldNodes) {
// (info, keepRoot)
tree = fieldTreeFromAST(info.fieldASTs, info.fragments)
tree = fieldTreeFromAST(fieldNodes, info.fragments)
if (!keepRoot) {
var key = firstKey(tree)
tree = tree[key]
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
},
"devDependencies": {
"chai": "^3.5.0",
"graphql": "^0.6.2",
"graphql": "^0.8.2",
"graphql-relay": "^0.4.4",
"istanbul": "^0.4.5",
"mocha": "^3.0.2",
"multiline": "^1.0.2",
"simple-relay-starter": "^1.3.11"
"multiline": "^1.0.2"
}
}
33 changes: 33 additions & 0 deletions test/fixtures/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// We use these types to hold data and resolve from GraphQL types in our schema

function User(id, name) {
this.id = id.toString()
this.name = name
}

function Widget(id, userId, name) {
this.id = id.toString()
this.userId = userId.toString()
this.name = name
}

// In a realistic system, the get functions below would return objects from a
// datastore like a DB or a REST API instead of an in-memory store like this.
// You can also return promises for async fetching

var users = [new User(1, 'Anonymous')]

var widgets = [
new Widget(1, 1, 'What\'s-it'),
new Widget(2, 1, 'Who\'s-it'),
new Widget(3, 1, 'How\'s-it'),
]

module.exports = {
User: User,
Widget: Widget,
getUser: function(id) { return users.filter(function(u) { return u.id == id })[0] },
getAnonymousUser: function() { return users[0] },
getWidget: function(id) { return widgets.filter(function(w) { return w.id == id })[0] },
getWidgetsByUser: function(userId) { return widgets.filter(function(w) { return w.userId == userId }) },
}
105 changes: 105 additions & 0 deletions test/fixtures/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// YANKED FROM https://github.com/mhart/simple-relay-starter/blob/master/schema/schema.js

var GraphQL = require('graphql')
var GraphQLRelay = require('graphql-relay')
var db = require('./database')

// This module exports a GraphQL Schema, which is a declaration of all the
// types, queries and mutations we'll use in our system.

// Relay adds some specific types that it needs to function, including Node, Edge, Connection

// Firstly we need to create the Node interface in our system. This has nothing
// to do with Node.js! In Relay, Node refers to an entity – that is, an object
// with an ID.

// To create this interface, we need to pass in a resolving function as the
// first arg to nodeDefinitions that can fetch an entity given a global Relay
// ID. The second arg can be used to resolve an entity into a GraphQL type –
// but it's actually optional, so we'll leave it out and use isTypeOf on the
// GraphQL types further below.

var nodeDefinitions = GraphQLRelay.nodeDefinitions(function(globalId) {
var idInfo = GraphQLRelay.fromGlobalId(globalId)
if (idInfo.type == 'User') {
return db.getUser(idInfo.id)
} else if (idInfo.type == 'Widget') {
return db.getWidget(idInfo.id)
}
return null
})

// We can now use the Node interface in the GraphQL types of our schema

var widgetType = new GraphQL.GraphQLObjectType({
name: 'Widget',
description: 'A shiny widget',

// Relay will use this function to determine if an object in your system is
// of a particular GraphQL type
isTypeOf: function(obj) { return obj instanceof db.Widget },

// We can either declare our fields as an object of name-to-definition
// mappings or a closure that returns said object (see userType below)
fields: {
id: GraphQLRelay.globalIdField('Widget'),
name: {
type: GraphQL.GraphQLString,
description: 'The name of the widget',
},
},
// This declares this GraphQL type as a Node
interfaces: [nodeDefinitions.nodeInterface],
})

var userType = new GraphQL.GraphQLObjectType({
name: 'User',
description: 'A person who uses our app',
isTypeOf: function(obj) { return obj instanceof db.User },

// We use a closure here because we need to refer to widgetType from above
fields: function() {
return {
id: GraphQLRelay.globalIdField('User'),
name: {
type: GraphQL.GraphQLString,
description: 'The name of the user',
},
// Here we set up a paged one-to-many relationship ("Connection")
widgets: {
description: 'A user\'s collection of widgets',

// Relay gives us helper functions to define the Connection and its args
type: GraphQLRelay.connectionDefinitions({name: 'Widget', nodeType: widgetType}).connectionType,
args: GraphQLRelay.connectionArgs,

// You can define a resolving function for any field.
// It can also return a promise if you need async data fetching
resolve: function(user, args) {
// This wraps a Connection object around your data array
// Use connectionFromPromisedArray if you return a promise instead
return GraphQLRelay.connectionFromArray(db.getWidgetsByUser(user.id), args)
},
},
}
},
interfaces: [nodeDefinitions.nodeInterface],
})

// Now we can bundle our types up and export a schema
// GraphQL expects a set of top-level queries and optional mutations (we have
// none in this simple example so we leave the mutation field out)
module.exports = new GraphQL.GraphQLSchema({
query: new GraphQL.GraphQLObjectType({
name: 'Query',
fields: {
// Relay needs this to query Nodes using global IDs
node: nodeDefinitions.nodeField,
// Our own root query field(s) go here
user: {
type: userType,
resolve: function() { return db.getAnonymousUser() },
},
},
}),
})
32 changes: 16 additions & 16 deletions test/graphql-parse-fields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ var multiline = require('multiline')
var parseFields = require('../')

var describe = global.describe
var beforeEach = global.beforeEach
var it = global.it

// https://github.com/mhart/simple-relay-starter/blob/master/schema/schema.js
var schema = require('simple-relay-starter/schema/schema.js')
var nestedFragmentsQuery = multiline(function(){/*
var schema = require('./fixtures/schema.js')
var nestedFragmentsQuery = multiline(function () { /*
query userQuery {
user {
...A
Expand Down Expand Up @@ -39,7 +39,7 @@ fragment B on User {
}
}
}
*/})
*/ })

describe('graphql-parse-fields', function () {
beforeEach(function () {
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('graphql-parse-fields', function () {
})
})

describe('keepRoot: true', function() {
describe('keepRoot: true', function () {
it('should parse info fields ast w/ nested fragments', function () {
var self = this
return graphql.graphql(schema, nestedFragmentsQuery, null, {}).then(function (data) {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('graphql-parse-fields', function () {
throw new Error('graphql error')
}
expect(self.info).to.exist
expect(parseFields(self.info.fieldASTs, self.info.fragments, { yolo: true })).to.deep.equal({
expect(parseFields(self.info.fieldNodes, self.info.fragments, { yolo: true })).to.deep.equal({
yolo: true,
user: {
id: true,
Expand All @@ -137,17 +137,17 @@ describe('graphql-parse-fields', function () {

it('should not error if it sees an invalid "kind"', function () {
var asts = {
"kind": "Field",
"alias": null,
"name": {
"kind": "Name",
"value": "user"
'kind': 'Field',
'alias': null,
'name': {
'kind': 'Name',
'value': 'user'
},
"selectionSet": {
"kind": "SelectionSet",
"selections": [
'selectionSet': {
'kind': 'SelectionSet',
'selections': [
{
"kind": "BOGUS_KIND"
'kind': 'BOGUS_KIND'
}
]
}
Expand All @@ -156,4 +156,4 @@ describe('graphql-parse-fields', function () {
user: {}
})
})
})
})