码迷,mamicode.com
首页 > 其他好文 > 详细

[GraphQL] Create a GraphQL Schema

时间:2016-12-28 09:46:18      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:require   ati   type   lan   query   bar   get   class   bool   

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.

[GraphQL] Create a GraphQL Schema

标签:require   ati   type   lan   query   bar   get   class   bool   

原文地址:http://www.cnblogs.com/Answer1215/p/6228190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!