aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-01-19 19:03:31 -0300
committerEuAndreh <eu@euandre.org>2021-01-19 19:03:31 -0300
commitbb8a1edb61ee0dddd6403ea82becb536fd4a3480 (patch)
tree0f89f0b50943f11e39fba4e3746ac53d24d2c10f
parente6174d4bf0cdb7ea42780c7e602c3d0d90f9d748 (diff)
downloadapollo-server-demo-bb8a1edb61ee0dddd6403ea82becb536fd4a3480.tar.gz
Add missing step, up until using authorization
-rw-r--r--.gitignore1
-rw-r--r--package-lock.json3
-rw-r--r--package.json3
-rw-r--r--src/00.js2
-rw-r--r--src/01.js2
-rw-r--r--src/02.js2
-rw-r--r--src/03.js2
-rw-r--r--src/04.js22
-rw-r--r--src/05.js34
-rw-r--r--src/06.js45
-rw-r--r--src/07.js54
11 files changed, 155 insertions, 15 deletions
diff --git a/.gitignore b/.gitignore
index 2ccbe46..6e046db 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/node_modules/
+/src/server-schema.json
diff --git a/package-lock.json b/package-lock.json
index a2386e4..e017348 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -5,7 +5,8 @@
"packages": {
"": {
"dependencies": {
- "apollo-server": "2.19.2"
+ "apollo-server": "2.19.2",
+ "apollo-server-express": "2.19.2"
}
},
"node_modules/@apollo/protobufjs": {
diff --git a/package.json b/package.json
index 977f1ec..4e0218e 100644
--- a/package.json
+++ b/package.json
@@ -1,5 +1,6 @@
{
"dependencies": {
- "apollo-server": "2.19.2"
+ "apollo-server": "2.19.2",
+ "apollo-server-express": "2.19.2"
}
}
diff --git a/src/00.js b/src/00.js
index 26cd087..f34af66 100644
--- a/src/00.js
+++ b/src/00.js
@@ -12,7 +12,7 @@ const server = new ApolloServer({
typeDefs,
});
-server.listen().then(({ url }) => {
+server.listen({ port: 4000 }).then(({ url }) => {
console.log(`Listening on ${url}
Given the above URL and the following GraphQL schema:
diff --git a/src/01.js b/src/01.js
index 8fa9774..2d702d7 100644
--- a/src/01.js
+++ b/src/01.js
@@ -17,7 +17,7 @@ const server = new ApolloServer({
resolvers,
});
-server.listen().then(({ url }) => {
+server.listen({ port: 4000 }).then(({ url }) => {
console.log(`Listening on ${url}
With the same curl as the previous step, the response is different.
diff --git a/src/02.js b/src/02.js
index 2cef3de..6c2c186 100644
--- a/src/02.js
+++ b/src/02.js
@@ -21,7 +21,7 @@ const server = new ApolloServer({
mocks: true,
});
-server.listen().then(({ url }) => {
+server.listen({ port: 4000 }).then(({ url }) => {
console.log(`Listening on ${url}
We have a new schema:
diff --git a/src/03.js b/src/03.js
index f16af0b..a594175 100644
--- a/src/03.js
+++ b/src/03.js
@@ -20,7 +20,7 @@ const server = new ApolloServer({
mocks: resolvers,
});
-server.listen().then(({ url }) => {
+server.listen({ port: 4000 }).then(({ url }) => {
console.log(`Listening on ${url}
Now the mocking + overrides are working again.
diff --git a/src/04.js b/src/04.js
index 35fbe6f..2d38c7d 100644
--- a/src/04.js
+++ b/src/04.js
@@ -1,17 +1,16 @@
-const { ApolloServer, gql } = require('apollo-server');
+const express = require('express');
+const { ApolloServer, gql } = require("apollo-server-express");
-const schema = `
+const typeDefs = gql`
type Query {
hello: String
goodbye: String
}
`;
-const typeDefs = gql(schema);
-
const resolvers = {
Query: () => ({
- hello: () => "hi",
+ hello: () => "hi still",
}),
};
@@ -20,9 +19,14 @@ const server = new ApolloServer({
mocks: resolvers,
});
-server.listen().then(({ url }) => {
- console.log(`Listening on ${url}
+const app = express();
+server.applyMiddleware({ app });
+
+const port = 4000;
+app.listen({ port }, () => {
+ console.log(`Listening on http://localhost:${port}${server.graphqlPath}
-woeifjw
-`);
+The code is refactored, but behaves the same.
+What changed?
+What is a middleware?`);
});
diff --git a/src/05.js b/src/05.js
new file mode 100644
index 0000000..e7bb584
--- /dev/null
+++ b/src/05.js
@@ -0,0 +1,34 @@
+const express = require('express');
+const { ApolloServer, gql } = require("apollo-server-express");
+
+const typeDefs = gql`
+ type Query {
+ hello: String
+ goodbye: String
+ }
+`;
+
+const resolvers = {
+ Query: () => ({
+ hello: () => "hi still",
+ }),
+};
+
+const server = new ApolloServer({
+ typeDefs,
+ mocks: resolvers,
+ playground: {},
+});
+
+const app = express();
+server.applyMiddleware({ app });
+
+const port = 4000;
+app.listen({ port }, () => {
+ console.log(`Listening on http://localhost:${port}${server.graphqlPath}
+
+We now have a "/ui"! Test it on the browser:
+http://localhost:${port}${server.graphqlPath}/ui
+
+How can it possibly do completion on the schema?`);
+});
diff --git a/src/06.js b/src/06.js
new file mode 100644
index 0000000..2100df3
--- /dev/null
+++ b/src/06.js
@@ -0,0 +1,45 @@
+const express = require('express');
+const { ApolloServer, gql } = require("apollo-server-express");
+const { buildClientSchema, printSchema } = require("graphql/utilities");
+const { makeExecutableSchema } = require("graphql-tools");
+const fs = require("fs");
+
+let schemaJSON;
+try {
+ schemaJSON = JSON.parse(fs.readFileSync("src/server-schema.json", "UTF-8"));
+} catch (e) {
+ if (e.code === "ENOENT") {
+ console.log(`File src/server-schema.json is missing.
+
+Try filling it with the schema of the server running in staging/production.`);
+ } else {
+ console.error(e);
+ }
+ process.exit(1);
+}
+
+const schemaTXT = printSchema(buildClientSchema(schemaJSON.data));
+const typeDefs = gql(schemaTXT);
+
+const resolvers = {
+ FIXME: () => ({
+ FIXME: () => "FIXME",
+ }),
+};
+
+const server = new ApolloServer({
+ typeDefs,
+ mocks: resolvers,
+ playground: {},
+});
+
+const app = express();
+server.applyMiddleware({ app });
+
+const port = 4000;
+app.listen({ port }, () => {
+ console.log(`Listening on http://localhost:${port}${server.graphqlPath}
+
+Now try using the UI and making queries.
+What if we add custom overrides to the "resolvers" map?`);
+});
diff --git a/src/07.js b/src/07.js
new file mode 100644
index 0000000..374ec8f
--- /dev/null
+++ b/src/07.js
@@ -0,0 +1,54 @@
+const express = require('express');
+const { ApolloServer, gql } = require("apollo-server-express");
+const { buildClientSchema, printSchema } = require("graphql/utilities");
+const { makeExecutableSchema } = require("graphql-tools");
+const fs = require("fs");
+
+let schemaJSON;
+try {
+ schemaJSON = JSON.parse(fs.readFileSync("src/server-schema.json", "UTF-8"));
+} catch (e) {
+ if (e.code === "ENOENT") {
+ console.log(`File src/server-schema.json is missing.
+
+Try filling it with the schema of the server running in staging/production.`);
+ } else {
+ console.error(e);
+ }
+ process.exit(1);
+}
+
+const schemaTXT = printSchema(buildClientSchema(schemaJSON.data));
+const typeDefs = gql(schemaTXT);
+
+const resolvers = {
+ FIXME: () => ({
+ FIXME: (_a, _b, context) => {
+ console.log(context);
+ if (context.authorization === "USER-A") {
+ return null;
+ } else {
+ return null;
+ }
+ },
+ }),
+};
+
+const server = new ApolloServer({
+ typeDefs,
+ mocks: resolvers,
+ playground: {},
+ context: ({ req }) => ({
+ authorization: (req.headers["authorization"] || "").replace(/Bearer /, "")
+ }),
+});
+
+const app = express();
+server.applyMiddleware({ app });
+
+const port = 4000;
+app.listen({ port }, () => {
+ console.log(`Listening on http://localhost:${port}${server.graphqlPath}
+
+Now we'll have to use custom headers that will be available when overriding.`);
+});