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