首页 > 其他 > 详细

[GraphQL] Create a GraphQL Schema

时间:2016-12-28 09:45:49      阅读:231      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!