Skip to content

Commit

Permalink
fix: the import of the api schema in withApollo and pages.api.index a…
Browse files Browse the repository at this point in the history
…fter module upgrade (#1295)
  • Loading branch information
ob6160 committed Mar 27, 2022
1 parent b674730 commit dc6456a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
27 changes: 27 additions & 0 deletions src/api-client/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const schema = (authDirective, redactedDirective) => {
const typeDefs = require('../api/typeDefs');
const resolvers = require('../api/resolvers');

const { makeExecutableSchema } = require('@graphql-tools/schema');

// Build our executable schema and apply our custom directives
const { redactedDirectiveTypeDefs, redactedDirectiveTransformer } =
redactedDirective('redact');
const { authDirectiveTypeDefs, authDirectiveTransformer } =
authDirective('auth');

return authDirectiveTransformer(
redactedDirectiveTransformer(
makeExecutableSchema({
typeDefs: [
redactedDirectiveTypeDefs,
authDirectiveTypeDefs,
typeDefs.default,
],
resolvers,
})
)
);
};

export default schema;
10 changes: 6 additions & 4 deletions src/api-client/withApollo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { NextPage } from 'next';
import merge from 'deepmerge';
import isEqual from 'lodash/isEqual';

import {
ApolloClient,
Expand All @@ -11,6 +9,8 @@ import {
} from '@apollo/client';

import { createPersistedQueryLink } from '@apollo/client/link/persisted-queries';
import redactedDirective from '../api/directives/redactedDirective';
import authDirective from '../api/directives/authDirective';
import { sha256 } from 'crypto-hash';

let apolloClient: ApolloClient<NormalizedCacheObject> | undefined;
Expand All @@ -19,8 +19,10 @@ function createApolloClient() {
let terminatingLink;
if (typeof window === 'undefined') {
const { SchemaLink } = require('@apollo/client/link/schema');
const { schema } = require('../pages/api');
terminatingLink = new SchemaLink({ schema });
const { default: schema } = require('./schema');
terminatingLink = new SchemaLink({
schema: schema(authDirective, redactedDirective),
});
} else {
terminatingLink = createHttpLink({
uri: '/api',
Expand Down
1 change: 0 additions & 1 deletion src/api/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { stringifyAndCompressLoos } from '../lib/loo';
import ngeohash from 'ngeohash';
import { async } from 'hasha';

const { Loo, Report, Area, MapGeo } = require('./db');
const { GraphQLDateTime } = require('graphql-iso-date');
Expand Down
24 changes: 5 additions & 19 deletions src/pages/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { ApolloServer } from 'apollo-server-micro';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
import { makeExecutableSchema } from '@graphql-tools/schema';
import jwt, { VerifyOptions } from 'jsonwebtoken';
import jwksClient from 'jwks-rsa';
import { getSession } from '@auth0/nextjs-auth0';
import Cors from 'cors';

import resolvers from '../../api/resolvers';
import redactedDirective from '../..//api/directives/redactedDirective';
import authDirective from '../../api/directives/authDirective';
import redactedDirective from '../../api/directives/redactedDirective';
import typeDefs from '../../api/typeDefs';
import schema from '../../api-client/schema';

const client = jwksClient({
jwksUri: `${process.env.AUTH0_ISSUER_BASE_URL}/.well-known/jwks.json`,
Expand All @@ -30,22 +27,11 @@ const options: VerifyOptions = {
algorithms: ['RS256'],
};

// Build our executable schema and apply our custom directives
const { redactedDirectiveTypeDefs, redactedDirectiveTransformer } =
redactedDirective('redact');
const { authDirectiveTypeDefs, authDirectiveTransformer } =
authDirective('auth');

export let schema = makeExecutableSchema({
typeDefs: [redactedDirectiveTypeDefs, authDirectiveTypeDefs, typeDefs],
resolvers,
});
schema = redactedDirectiveTransformer(schema);
schema = authDirectiveTransformer(schema);

// Add GraphQL API
const finalSchema = schema(authDirective, redactedDirective);

export const server = new ApolloServer({
schema,
schema: finalSchema,
context: async ({ req, res }) => {
let user = null;
// TODO: why does this throw an error?
Expand Down

0 comments on commit dc6456a

Please sign in to comment.