Skip to content

Commit

Permalink
Merge pull request #29 from codeday/account
Browse files Browse the repository at this point in the history
add support for codeday/account-gql
  • Loading branch information
Nexite authored May 8, 2022
2 parents 2329e12 + 0d53ab2 commit f1a07c2
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 2,186 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
"apollo-server": "^2.25.3",
"apollo-server-express": "^2.4.8",
"apollo-upload-client": "^14.1.3",
"auth0": "^2.27.1",
"auth0-get-all-users": "^1.1.0",
"babel-loader": "^8.0.5",
"body-parser": "^1.18.3",
"cross-fetch": "^3.0.6",
Expand Down
8 changes: 4 additions & 4 deletions src/remoteTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import extractFiles from 'extract-files/public/extractFiles';
// HTTP requests
//

function makeExecutor(httpEndpoint) {
function makeExecutor(httpEndpoint, prefix) {
return async function executor({ document, variables, context }) {
const allowedHeaders = Object.keys(context?.headers || {})
.filter((name) => name.toLowerCase().substr(0, 2) === 'x-')
.filter((name) => name.toLowerCase().startsWith(prefix))
.reduce((accum, name) => ({ ...accum, [name]: context?.headers[name] }), {});

const query = print(document);
Expand Down Expand Up @@ -118,10 +118,10 @@ function makeSubscriber(wsEndpoint) {
};
}

export default function makeRemoteTransport(httpEndpoint, wsEndpoint) {
export default function makeRemoteTransport(httpEndpoint, wsEndpoint, prefix="x-") {

return {
executor: makeExecutor(httpEndpoint),
executor: makeExecutor(httpEndpoint, prefix),
subscriber: wsEndpoint ? makeSubscriber(wsEndpoint) : undefined,
};
}
118 changes: 118 additions & 0 deletions src/remotes/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { wrapSchema, introspectSchema } from '@graphql-tools/wrap';
import makeRemoteTransport from '../remoteTransport';
import { delegateToSchema } from '@graphql-tools/delegate';
import { TransformQuery } from '@graphql-tools/wrap';
import { Kind } from 'graphql';

function getConnectionTypes(prefix) {
return `
extend type ${prefix}Badge {
details: CmsBadge
}
extend type ${prefix}User {
sites: [CmsSite]
}
`;
}

function getConnectionResolvers(prefix, schemas) {
return {
[`${prefix}Badge`]: {
details: {
selectionSet: '{ id }',
async resolve(parent, args, context, info) {
return delegateToSchema({
schema: schemas.cms,
operation: 'query',
fieldName: 'badgeCollection',
args: {
where: {
id: parent.id,
},
limit: 1,
},
context,
info,
transforms: [
new TransformQuery({
path: ['badgeCollection'],
queryTransformer: (subtree) => ({
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: 'items',
},
selectionSet: subtree,
},
],
}),
resultTransformer: (r) => r?.items[0],
}),
],
});
},
},
},
[`${prefix}User`]: {
sites: {
selectionSet: '{ roles }',
async resolve(parent, args, context, info) {
return delegateToSchema({
schema: schemas.cms,
operation: 'query',
fieldName: 'siteCollection',
args: {
where: {
OR: [...parent.roles.map((role) => {
if (role.name == "Staff") return { type: "Volunteer" };
return { type: role.name };
}), { type: "Student" }]
}
},
context,
info,
transforms: [
new TransformQuery({
path: ['siteCollection'],
queryTransformer: (subtree) => ({
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: 'items',
},
selectionSet: subtree,
},
],
}),
resultTransformer: (r) => r?.items,
}),
],
});
},
},
},
};
}

export default async function createAccountSchema(uri, wsUri) {
console.log(` * account(${uri})`);
const { executor, subscriber } = makeRemoteTransport(uri, wsUri, "");
const schema = wrapSchema({
schema: await introspectSchema(executor),
executor,
subscriber,
});
return {
schema,
transforms: [],
getConnectionTypes,
getConnectionResolvers,
};
}
Loading

0 comments on commit f1a07c2

Please sign in to comment.