Last active
February 13, 2025 09:36
-
-
Save xpepermint/7376b8c67caa926e19d2 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" | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
} |
Thank you for sharing this - it is helpful.
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
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