[GRAPHQL] Write a graphql Schema in JavaScript

Source: Internet
Author: User

Writing out a graphql Schema in the common graphql Language can work for simple graphql Schemas, but as our application gr oWS, or when we start using the more complex types like interfaces or unions, we find the We can ' t use a GRAPHQL Language fil E in the same as before. In this video, we'll learn how to translate a GRAPHQL schema written in graphql into a GRAPHQL schema written in Javascrip T.

ConstExpress = require ('Express');ConstGraphqlhttp = require ('express-graphql');ConstServer =Express ();ConstPort = Process.env.PORT | | the;Const{graphqlschema, Graphqlobjecttype, graphqlstring, Graphqlint, Graphqlboolean, graphqlid} 
    = Require ('graphql');ConstVideotype =NewGraphqlobjecttype ({name:'Video', Description:'A Video on Egghead.io', fields: {ID: {type:graphqlid, Description:'The ID of the video'}, Title: {type:graphqlstring, Description:'The title of the video'}, Duration: {type:graphqlint, Description:'The duration of the video'}, watched: {Type:graphqlboolean, Description:'Whether or no the viewer watched the video'        }    }})ConstQueryType =NewGraphqlobjecttype ({name:'QueryType', Description:'The root query type', fields: {video: {type:videotype, Resolve: ()= {                return NewPromise ((Resolve) ={resolve ({ID:'a', Title:'graphql', Duration: the, watched:false })                })            }        }    }});Constschema =NewGraphqlschema ({query:querytype});/*Const SCHEMA = Buildschema (' type Video {id:id, title:string, Duration:int, watched : Boolean} type Query {video:video, videos: [VIDEO]} type schema{Query:quer y} '); const VIDEOS = [{id: ' 1 ', title: ' React ', duration:180, watched: True}, {id: ' 2 ', title: ' Relay ', duration:230, Watched:false}];con St resolvers = {video: () = ({id: ' 1 ', title: ' Bar ', duration:180, wat Ched:true}), Videos: () = videos};*/Server.use ('/graphql', Graphqlhttp ({schema, GRAPHIQL:true,//Use GRAPHIQL interface}); Server.listen (port, ()={console.log (' Listening on HTTP ')})

[GRAPHQL] Write a graphql Schema in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.