Forked from xpepermint/graphql-nested-schema-example
Created
November 18, 2017 22:48
-
-
Save ohmyhusky/c38bb7acbac1bfe6c0204b8efd0cd032 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Build GraphQL server using `express-graphql` package. | |
2. Configure `schema.js` file. | |
3. Query for data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -XPOST -H "Content-Type:application/graphql" -d ' | |
query RootQuery { | |
project(id: 2) { | |
name | |
} | |
projects { | |
id | |
name | |
members { | |
id | |
name | |
tickets { | |
message | |
} | |
} | |
} | |
}' http://localhost:4444/graphql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLInt, | |
GraphQLList, | |
GraphQLID, | |
GraphQLNonNull | |
} from 'graphql/type'; | |
const TicketType = new GraphQLObjectType({ | |
name: 'TicketType', | |
fields: { | |
id: { | |
type: GraphQLString | |
}, | |
message: { | |
type: GraphQLString | |
} | |
} | |
}); | |
const MemberType = new GraphQLObjectType({ | |
name: 'MemberType', | |
fields: { | |
id: { | |
type: GraphQLInt | |
}, | |
name: { | |
type: GraphQLString | |
}, | |
tickets: { | |
type: new GraphQLList(TicketType), | |
resolve(member) { | |
return [ | |
{id: 1, message: `Member: ${member.id}, Ticket: 1`}, | |
{id: 2, message: `Member: ${member.id}, Ticket: 2`} | |
]; | |
} | |
} | |
} | |
}); | |
const ProjectType = new GraphQLObjectType({ | |
name: 'ProjectType', | |
fields: { | |
id: { | |
type: GraphQLInt | |
}, | |
name: { | |
type: GraphQLString | |
}, | |
members: { | |
type: new GraphQLList(MemberType), | |
resolve(project) { | |
return [ | |
{id: 1, name: `Project: ${project.id}, Member: 1`}, | |
{id: 2, name: `Project: ${project.id}, Member: 2`} | |
]; | |
} | |
} | |
} | |
}); | |
const RootType = new GraphQLObjectType({ | |
name: 'RootType', | |
fields: { | |
projects: { | |
type: new GraphQLList(ProjectType), | |
resolve() { | |
return [ | |
{id: 1, name: `Project 1`}, | |
{id: 2, name: `Project 2`} | |
]; | |
} | |
}, | |
project: { | |
type: ProjectType, | |
args: { | |
id: { | |
type: new GraphQLNonNull(GraphQLID) | |
} | |
}, | |
resolve(parent, {id}) { | |
return {id: id, name: `Project ${id}`} | |
} | |
} | |
} | |
}); | |
const schema = new GraphQLSchema({ | |
query: RootType | |
}); | |
export default schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"projects": [ | |
{ | |
"id": 1, | |
"name": "Project 1", | |
"members": [ | |
{ | |
"id": 1, | |
"name": "Project: 1, Member: 1", | |
"tickets": [ | |
{ | |
"id": "1", | |
"message": "Member: 1, Ticket: 1" | |
}, | |
{ | |
"id": "2", | |
"message": "Member: 1, Ticket: 2" | |
} | |
] | |
}, | |
{ | |
"id": 2, | |
"name": "Project: 1, Member: 2", | |
"tickets": [ | |
{ | |
"id": "1", | |
"message": "Member: 2, Ticket: 1" | |
}, | |
{ | |
"id": "2", | |
"message": "Member: 2, Ticket: 2" | |
} | |
] | |
} | |
] | |
}, | |
{ | |
"id": 2, | |
"name": "Project 2", | |
"members": [ | |
{ | |
"id": 1, | |
"name": "Project: 2, Member: 1", | |
"tickets": [ | |
{ | |
"id": "1", | |
"message": "Member: 1, Ticket: 1" | |
}, | |
{ | |
"id": "2", | |
"message": "Member: 1, Ticket: 2" | |
} | |
] | |
}, | |
{ | |
"id": 2, | |
"name": "Project: 2, Member: 2", | |
"tickets": [ | |
{ | |
"id": "1", | |
"message": "Member: 2, Ticket: 1" | |
}, | |
{ | |
"id": "2", | |
"message": "Member: 2, Ticket: 2" | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment