Skip to content

Instantly share code, notes, and snippets.

@xpepermint
Last active February 13, 2025 09:36
Show Gist options
  • Save xpepermint/7376b8c67caa926e19d2 to your computer and use it in GitHub Desktop.
Save xpepermint/7376b8c67caa926e19d2 to your computer and use it in GitHub Desktop.

Revisions

  1. xpepermint revised this gist Sep 18, 2015. 2 changed files with 19 additions and 5 deletions.
    7 changes: 4 additions & 3 deletions graphql: command-line-request
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,17 @@
    curl -XPOST -H "Content-Type:application/graphql" -d '
    query RootQuery {
    project(id: 2) {
    name
    }
    projects {
    id
    name
    members {
    id
    name
    tickets {
    id
    message
    }
    }
    }
    }
    ' http://localhost:4444/graphql
    }' http://localhost:4444/graphql
    17 changes: 15 additions & 2 deletions graphql: schema.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@ import {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLList
    GraphQLList,
    GraphQLID,
    GraphQLNonNull
    } from 'graphql/type';

    const TicketType = new GraphQLObjectType({
    @@ -71,6 +73,17 @@ const RootType = new GraphQLObjectType({
    {id: 2, name: `Project 2`}
    ];
    }
    },
    project: {
    type: ProjectType,
    args: {
    id: {
    type: new GraphQLNonNull(GraphQLID)
    }
    },
    resolve(parent, {id}) {
    return {id: id, name: `Project ${id}`}
    }
    }
    }
    });
    @@ -79,4 +92,4 @@ const schema = new GraphQLSchema({
    query: RootType
    });

    export default schema;
    export default schema;
  2. xpepermint revised this gist Sep 18, 2015. 4 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  3. xpepermint revised this gist Sep 18, 2015. 4 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  4. xpepermint revised this gist Sep 18, 2015. 4 changed files with 175 additions and 5 deletions.
    16 changes: 16 additions & 0 deletions command-line-request
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    curl -XPOST -H "Content-Type:application/graphql" -d '
    query RootQuery {
    projects {
    id
    name
    members {
    id
    name
    tickets {
    id
    message
    }
    }
    }
    }
    ' http://localhost:4444/graphql
    8 changes: 3 additions & 5 deletions graphql-nested-schema-example
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    // GraphQL schema example

    ```js
    console.log(111)
    ```
    1. Build GraphQL server using `express-graphql` package.
    2. Configure `schema.js` file.
    3. Query for data.
    82 changes: 82 additions & 0 deletions schema.sj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    import {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLList
    } 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`}
    ];
    }
    }
    }
    });

    const schema = new GraphQLSchema({
    query: RootType
    });

    export default schema;
    74 changes: 74 additions & 0 deletions server-response
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    {
    "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"
    }
    ]
    }
    ]
    }
    ]
    }
    }
  5. xpepermint revised this gist Sep 18, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion graphql-nested-schema-example
    Original file line number Diff line number Diff line change
    @@ -1 +1,5 @@
    // GraphQL schema example
    // GraphQL schema example

    ```js
    console.log(111)
    ```
  6. xpepermint created this gist Sep 18, 2015.
    1 change: 1 addition & 0 deletions graphql-nested-schema-example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    // GraphQL schema example