From ee6c5bf329b2ece3a007c8bda27a2f0abee1b0aa Mon Sep 17 00:00:00 2001 From: MrDoomBringer Date: Thu, 28 Jul 2022 22:15:20 -0400 Subject: [PATCH 1/2] Update graphql version, dependabot, file org --- .github/dependabot.yml | 5 +++ package.json | 2 +- src/index.jsx | 83 +----------------------------------------- src/link.js | 27 ++++++++++++++ src/schema.js | 55 ++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 82 deletions(-) create mode 100644 src/link.js create mode 100644 src/schema.js diff --git a/.github/dependabot.yml b/.github/dependabot.yml index de983a50..070271f3 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -7,3 +7,8 @@ updates: directory: "/" # Location of package manifests schedule: interval: "daily" + ignore: + - dependency-name: "graphql" + # Currently the graphql module seems to break on Codesandbox when + # over version 16, so we'll keep it on 15.x until this is resolved. + # See issue apollo-client/#9943. diff --git a/package.json b/package.json index 883fe55a..4a2318f8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "MIT", "dependencies": { "@apollo/client": "^3.6.8", - "graphql": "^16.4.0", + "graphql": "^15.6.1", "react": "^18.2.0", "react-dom": "^18.2.0" }, diff --git a/src/index.jsx b/src/index.jsx index f3b739ea..f1ad30f8 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -1,84 +1,3 @@ -/*** SCHEMA ***/ -import { - GraphQLSchema, - GraphQLObjectType, - GraphQLID, - GraphQLString, - GraphQLList, -} from 'graphql'; -const PersonType = new GraphQLObjectType({ - name: 'Person', - fields: { - id: { type: GraphQLID }, - name: { type: GraphQLString }, - }, -}); - -const peopleData = [ - { id: 1, name: 'John Smith' }, - { id: 2, name: 'Sara Smith' }, - { id: 3, name: 'Budd Deey' }, -]; - -const QueryType = new GraphQLObjectType({ - name: 'Query', - fields: { - people: { - type: new GraphQLList(PersonType), - resolve: () => peopleData, - }, - }, -}); - -const MutationType = new GraphQLObjectType({ - name: 'Mutation', - fields: { - addPerson: { - type: PersonType, - args: { - name: { type: GraphQLString }, - }, - resolve: function (_, { name }) { - const person = { - id: peopleData[peopleData.length - 1].id + 1, - name, - }; - - peopleData.push(person); - return person; - } - }, - }, -}); - -const schema = new GraphQLSchema({ query: QueryType, mutation: MutationType }); - -/*** LINK ***/ -import { graphql, print } from "graphql"; -import { ApolloLink, Observable } from "@apollo/client"; -function delay(wait) { - return new Promise(resolve => setTimeout(resolve, wait)); -} - -const link = new ApolloLink(operation => { - return new Observable(async observer => { - const { query, operationName, variables } = operation; - await delay(300); - try { - const result = await graphql({ - schema, - source: print(query), - variableValues: variables, - operationName, - }); - observer.next(result); - observer.complete(); - } catch (err) { - observer.error(err); - } - }); -}); - /*** APP ***/ import React, { useState } from "react"; import { createRoot } from "react-dom/client"; @@ -90,6 +9,8 @@ import { useQuery, useMutation, } from "@apollo/client"; + +import { link } from "./link.js"; import "./index.css"; const ALL_PEOPLE = gql` diff --git a/src/link.js b/src/link.js new file mode 100644 index 00000000..b986d237 --- /dev/null +++ b/src/link.js @@ -0,0 +1,27 @@ +/*** LINK ***/ +import { graphql, print } from "graphql"; +import { ApolloLink, Observable } from "@apollo/client"; +import { schema } from "./schema.js"; + +function delay(wait) { + return new Promise(resolve => setTimeout(resolve, wait)); +} + +export const link = new ApolloLink(operation => { + return new Observable(async observer => { + const { query, operationName, variables } = operation; + await delay(300); + try { + const result = await graphql({ + schema, + source: print(query), + variableValues: variables, + operationName, + }); + observer.next(result); + observer.complete(); + } catch (err) { + observer.error(err); + } + }); +}); diff --git a/src/schema.js b/src/schema.js new file mode 100644 index 00000000..8fa2431b --- /dev/null +++ b/src/schema.js @@ -0,0 +1,55 @@ +/*** SCHEMA ***/ +import { + GraphQLSchema, + GraphQLObjectType, + GraphQLID, + GraphQLString, + GraphQLList, +} from 'graphql'; + +const PersonType = new GraphQLObjectType({ + name: 'Person', + fields: { + id: { type: GraphQLID }, + name: { type: GraphQLString }, + }, +}); + +const peopleData = [ + { id: 1, name: 'John Smith' }, + { id: 2, name: 'Sara Smith' }, + { id: 3, name: 'Budd Deey' }, +]; + +const QueryType = new GraphQLObjectType({ + name: 'Query', + fields: { + people: { + type: new GraphQLList(PersonType), + resolve: () => peopleData, + }, + }, +}); + +const MutationType = new GraphQLObjectType({ + name: 'Mutation', + fields: { + addPerson: { + type: PersonType, + args: { + name: { type: GraphQLString }, + }, + resolve: function (_, { name }) { + const person = { + id: peopleData[peopleData.length - 1].id + 1, + name, + }; + + peopleData.push(person); + return person; + } + }, + }, +}); + +export const schema = new GraphQLSchema({ query: QueryType, mutation: MutationType }); From c32e69cb73e6c707cffa6b27c6dbe86ec958b048 Mon Sep 17 00:00:00 2001 From: MrDoomBringer Date: Thu, 28 Jul 2022 22:29:22 -0400 Subject: [PATCH 2/2] Add index.jsx to package entrypoint --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4a2318f8..e8bc8a00 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "name": "apollo-client-error-template", "version": "1.0.0", "license": "MIT", + "main": "src/index.jsx", "dependencies": { "@apollo/client": "^3.6.8", "graphql": "^15.6.1",