[GraphQL] Create a GraphQL Schema

we’ll take a look at the GraphQL Language and write out our first GraphQL Schema. We’ll use the graphql package available to us through npm to parse our graphql language file and resolve our initial query.

const { graphql, buildSchema } = require(‘graphql‘);

const schema = buildSchema(`
    type Query {
        id: ID,
        title: String,
        duration: Int,
        watched: Boolean
    }

    type Schema{
        query: Query
    }
`);

const resolvers = {
    id       : () => ‘1‘,
    title    : () => ‘bar‘,
    duration : () => 180,
    watched  : true
};

const query = `
    query myFirstQuery {
        id,
        title,
        duration,
        watched
    }
`;

graphql(schema, query, resolvers)
.then((result) => console.log(result))
.catch(console.error)

We pass in the query we want, GraphQL will verify the query based on the schema we pass in. If it is ok, then will get data from resolver.

时间: 2024-10-20 11:15:31

[GraphQL] Create a GraphQL Schema的相关文章

[GraphQL] Write a GraphQL Schema in JavaScript

Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in t

[GraphQL] Deploy a GraphQL dev playground with graphql-up

In this lesson we'll use a simple GraphQL IDL schema to deploy and explore a fully functional GraphQL service in minutes with graphql-up. Install: npm i -g graphql-up -g Create schema: type Person { id: ID!, name: String!, tasks: [Task!]! @relation(n

[GraphQL] Create an Input Object Type for Complex Mutations

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type. const express = r

[GraphQL] Query a GraphQL API with graphql-request

To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation in the body of the request. In this lesson, we will use the browser’s fetch method to request the total days skied from our GraphQL API. const query

[GraphQL] Use GraphQLList with GraphQLObject Types

When working with collections of things in GraphQL, we'll always reach out for the GraphQLListType. In this video, we'll learn how to use GraphQLList from the graphql package in combination with a GraphQLObject Type to create a field that returns a c

[GraphQL] Use GraphQL's Object Type for Basic Types

We can create the most basic components of our GraphQL Schema using GraphQL's Object Types. These types allow us to group related fields together under a specific type, such as a Video or a User, and then allows us to fetch these types when we query

GraphQL介绍&使用nestjs构建GraphQL查询服务

GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接看查询语句就能知道查询出来的数据是什么样的.本质上属于API Layer层,负责前端请求的合并.数据整理等功能. 查询示例 使用几个简单的例子看下GraphQL的查询是什么样子的. 普通查询 { me { name } } 查询出来的数据格式如下: { "me": { "name

Modularizing your graphQL schemas

转自: https://tomasalabes.me/blog/nodejs/graphql/2018/07/11/modularizing-graphql.html Modularizing your graphQL schemas Working in a kinda big graphql server, schemas can easily get out of hand with all the types, inputs, queries, mutations, subscripti

Why GraphQL is Taking Over APIs

A few years ago, I managed a team at DocuSign that was tasked with re-writing the main DocuSign web app which was used by tens of millions of users. The APIs didn’t exist yet to support our new shiny front-end app because since the beginning the web