Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
1. Build GraphQL server using `express-graphql` package.
2. Configure `schema.js` file.
3. Query for data.
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
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;
{
"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"
}
]
}
]
}
]
}
}
@sub-narayanan
Copy link

sub-narayanan commented Dec 20, 2017

Hi Kristijan, I am trying to learn GraphQL and need some help. Using your code, I wanted to embed Members inside Projects (subdocument) and then connect them to Tickets. Also I wanted to use discriminator feature for two types of Tickets. But I am not able to resolve Tickets. If I send you my zip file, can you tell me what I am doing wrong? Thanks in advance ... it might take just a few minutes of your time, but it would really help me a lot. Warm Regards, Narayanan

@azivkovi
Copy link

azivkovi commented Nov 28, 2020

Thank you for sharing this - it is helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment