GraphQL schemas: introduction


        #GraphQL Schema
        const Query = new GraphQLObjectType({
            name: 'NewsSchema',
            description: "Root of the News Schema",
            fields: () => ({
                posts: {
                    type: new GraphQLList(Post),
                    resolve: function() {
                        return PostsList;
                    }
                }
            })
        });

        const Schema = new GraphQLSchema({
            query: Query
        });
    

GraphQL schemas: GraphQLObjectType


        const Author = new GraphQLObjectType({
            name: "Author",
            description: "An author",
            fields: () => ({
                _id: {type: new GraphQLNonNull(GraphQLString)},
                name: {type: new GraphQLNonNull(GraphQLString)}
            })
        });

        const Post = new GraphQLObjectType({
            name: "Post",
            description: "A news Post",
            fields: () => ({
                _id: {GraphQLString},
                title: {type: new GraphQLNonNull(GraphQLString)},
                content: {type: new GraphQLNonNull(GraphQLString)},
                author: {
                    type: Author,
                    resolve: function(post) {
                        return AuthorsList.find(a => a._id == post.author) || "anonymous author"; // our responsability when non trivial + return default value
                    }
                }
            })
        });
    

GraphQL Schema Type syntax


        type Task {
            _id: String!
            title: String!
            isCompleted: Boolean!
        }

        type TaskList {
            _id: String!
            name: String!
            tasks: [Task]!
        }

        schema {
            query: RootQuery
            mutation: RootMutation
        }

        # ///////////////////////////////////////////////////////////////////
        # //                        Querie                                 //
        # ///////////////////////////////////////////////////////////////////

        type RootQuery {
            getAllTaskList: [TaskList]
        }

        # ///////////////////////////////////////////////////////////////////
        # //                       Mutation                                //
        # ///////////////////////////////////////////////////////////////////

        type RootMutation {
            createTask(input: CreateTaskInput!): Task
            removeTask(input: RemoveTaskInput!): Boolean
            updateTaskTitle(input: UpdateTaskTitleInput!): Task
            toggleTask(input: ToggleTaskInput!): Task
            createList(input: CreateTaskListInput!): TaskList
            updateTaskListName(input: UpdateTaskListNameInput!): TaskList
            clearCompleted(input: ClearCompletedTaskListInput!): TaskList
        }

        input CreateTaskInput {
            _taskListId: String!
            title: String!
            isCompleted: Boolean
        }

        input RemoveTaskInput {
            _taskListId: String!
            _id: String!
        }

        # // This is a specific update mutation instead of a general one, so I
        # // don’t nest with a "patch"
        # // Instead we just provide one field, "title", which signals intent.
        input UpdateTaskTitleInput {
            _taskListId: String!
            _id: String!
            title: String!
        }

        input ToggleTaskInput {
            _taskListId: String!
            _id: String!
        }

        # "id" is generated by the backend, and "isCompleted" is automatically
        # set to false.
        input CreateTaskListInput {
            name: String!
        }

        input UpdateTaskListNameInput {
            _id: String!
            name: String!
        }

        input ClearCompletedTaskListInput {
            _id: String!
        }

        # Example recommandation just wrap to foresee the future
        # type CreateTaskOutput {
        #   The task that was created. It is nullable so that if there is
        #   an error then null won’t propagate past the "task".
        #   task: Task
        # }
    

GraphQL Type

GraphQL Interface & Union