Forráskód Böngészése

Video 6: write a schema in JavaScript instead of GraphQL.

Frederic G. MARAND 7 éve
szülő
commit
127dad5891
2 módosított fájl, 75 hozzáadás és 103 törlés
  1. 21 84
      .idea/workspace.xml
  2. 54 19
      index.js

+ 21 - 84
.idea/workspace.xml

@@ -4,7 +4,6 @@
     <list default="true" id="417b6c39-7b77-43c5-80fb-b9bf95b57f36" name="Default" comment="">
       <change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
       <change type="MODIFICATION" beforePath="$PROJECT_DIR$/index.js" afterPath="$PROJECT_DIR$/index.js" />
-      <change type="MODIFICATION" beforePath="$PROJECT_DIR$/package.json" afterPath="$PROJECT_DIR$/package.json" />
     </list>
     <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
     <option name="TRACKING_ENABLED" value="true" />
@@ -22,33 +21,9 @@
       <file leaf-file-name="index.js" pinned="false" current-in-tab="true">
         <entry file="file://$PROJECT_DIR$/index.js">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="308">
-              <caret line="22" column="0" lean-forward="true" selection-start-line="22" selection-start-column="0" selection-end-line="22" selection-end-column="0" />
-              <folding>
-                <marker date="1481978137000" expanded="true" signature="241:318" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="246:251" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="252:318" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="256:258" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="260:262" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="266:271" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="273:279" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="283:291" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="293:296" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="300:307" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="309:316" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="320:367" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="325:330" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="331:367" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="335:340" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="342:347" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="350:356" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="359:364" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="369:399" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="374:380" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="381:399" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="385:390" ph="{...}" />
-                <marker date="1481978137000" expanded="true" signature="392:397" ph="{...}" />
-              </folding>
+            <state relative-caret-position="98">
+              <caret line="7" column="12" lean-forward="true" selection-start-line="7" selection-start-column="12" selection-end-line="7" selection-end-column="12" />
+              <folding />
             </state>
           </provider>
         </entry>
@@ -98,6 +73,16 @@
     <panes>
       <pane id="ProjectPane">
         <subPane>
+          <PATH>
+            <PATH_ELEMENT>
+              <option name="myItemId" value="Egghead GraphQL" />
+              <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
+            </PATH_ELEMENT>
+            <PATH_ELEMENT>
+              <option name="myItemId" value="GraphQL Schemas" />
+              <option name="myItemType" value="com.intellij.lang.jsgraphql.schema.ide.project.JSGraphQLSchemaDirectoryNode" />
+            </PATH_ELEMENT>
+          </PATH>
           <PATH>
             <PATH_ELEMENT>
               <option name="myItemId" value="Egghead GraphQL" />
@@ -237,12 +222,12 @@
       <updated>1481822369870</updated>
       <workItem from="1481822371153" duration="34000" />
       <workItem from="1481822430451" duration="270000" />
-      <workItem from="1481969401352" duration="2700000" />
+      <workItem from="1481969401352" duration="4966000" />
     </task>
     <servers />
   </component>
   <component name="TimeTrackingManager">
-    <option name="totallyTimeSpent" value="3004000" />
+    <option name="totallyTimeSpent" value="5270000" />
   </component>
   <component name="ToolWindowManager">
     <frame x="0" y="23" width="1435" height="877" extended-state="6" />
@@ -252,9 +237,9 @@
       <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="true" content_ui="tabs" />
       <window_info id="GraphQL" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.3299363" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" />
       <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
-      <window_info id="Run" active="true" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.2089172" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
+      <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.2089172" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
       <window_info id="Mongo Explorer" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
-      <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.22929937" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
+      <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.22929937" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
       <window_info id="Project" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.23436376" sideWeight="0.5" order="5" side_tool="false" content_ui="combo" />
       <window_info id="Docker" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="11" side_tool="false" content_ui="tabs" />
       <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
@@ -286,31 +271,7 @@
       <provider selected="true" editor-type-id="text-editor">
         <state relative-caret-position="0">
           <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
-          <folding>
-            <marker date="1481978137000" expanded="true" signature="241:318" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="246:251" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="252:318" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="256:258" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="260:262" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="266:271" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="273:279" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="283:291" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="293:296" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="300:307" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="309:316" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="320:367" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="325:330" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="331:367" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="335:340" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="342:347" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="350:356" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="359:364" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="369:399" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="374:380" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="381:399" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="385:390" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="392:397" ph="{...}" />
-          </folding>
+          <folding />
         </state>
       </provider>
     </entry>
@@ -348,33 +309,9 @@
     </entry>
     <entry file="file://$PROJECT_DIR$/index.js">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="308">
-          <caret line="22" column="0" lean-forward="true" selection-start-line="22" selection-start-column="0" selection-end-line="22" selection-end-column="0" />
-          <folding>
-            <marker date="1481978137000" expanded="true" signature="241:318" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="246:251" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="252:318" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="256:258" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="260:262" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="266:271" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="273:279" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="283:291" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="293:296" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="300:307" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="309:316" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="320:367" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="325:330" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="331:367" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="335:340" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="342:347" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="350:356" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="359:364" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="369:399" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="374:380" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="381:399" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="385:390" ph="{...}" />
-            <marker date="1481978137000" expanded="true" signature="392:397" ph="{...}" />
-          </folding>
+        <state relative-caret-position="98">
+          <caret line="7" column="12" lean-forward="true" selection-start-line="7" selection-start-column="12" selection-end-line="7" selection-end-column="12" />
+          <folding />
         </state>
       </provider>
     </entry>

+ 54 - 19
index.js

@@ -2,29 +2,65 @@
 
 const express = require('express');
 const graphqlHTTP = require('express-graphql');
-const { buildSchema } = require('graphql');
+const {
+  GraphQLSchema,
+  GraphQLObjectType,
+  GraphQLID,
+  GraphQLString,
+  GraphQLInt,
+  GraphQLBoolean
+} = require('graphql');
 
 const PORT = process.env.PORT || 3000;
 
 const server = express();
 
-const schema = buildSchema(`
-type Video {
-  id: ID,
-  title: String,
-  duration: Int,
-  watched: Boolean
-}
-
-type Query {
-  video: Video
-  videos: [Video]
-}
+const videoType = new GraphQLObjectType({
+  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, in seconds',
+    },
+    watched: {
+      type: GraphQLBoolean,
+      description: 'Whether or not the viewer has watched the video'
+    }
+  }
+});
+const queryType = new GraphQLObjectType({
+  name: 'QueryType',
+  description: 'The root query  type',
+  fields: {
+    video: {
+      type: videoType,
+      resolve: () => new Promise((resolve) => {
+        resolve({
+          id: 'a',
+          title: 'GraphQL',
+          duration:  180,
+          watched: false
+        });
+      })
+    }
+  }
+});
 
-type Schema {
-  query: Query
-}
-`);
+const schema = new GraphQLSchema({
+  query: queryType,
+  // Also available:
+  // mutation: ...
+  // subscription: ...
+});
 
 const videoA = {
   id: 'a',
@@ -54,8 +90,7 @@ const resolvers = {
 
 server.use('/graphql', graphqlHTTP({
   schema,
-  graphiql: true,
-  rootValue: resolvers
+  graphiql: true
 }));
 
 server.listen(PORT, () => {