Skip to content

Commit

Permalink
chore: Merge PR #52 from feat/add-graphql-union-tuple-support
Browse files Browse the repository at this point in the history
  • Loading branch information
codinsonn authored Nov 1, 2023
2 parents 983d144 + 9850043 commit bead934
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jobs:
env:
CHROMATIC_PROJECT_TOKEN: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}

- name: Setup Node
if: ${{ steps.check-secrets.outputs.has-secrets == 'true' }}
uses: actions/setup-node@v2
with:
node-version: 18.x
cache: yarn

- name: 'Warn required secrets'
if: ${{ steps.check-secrets.outputs.has-secrets == 'false' }}
run: echo 'CHROMATIC_PROJECT_TOKEN missing in Github secrets. Skipping Storybook deployment.'
Expand Down
10 changes: 10 additions & 0 deletions apps/next/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
scalar Date

"""
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSON

"""
The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSONObject

input GetResumeDataByUserSlugArgs {
"""
the unique slug for a user's resume. Can be used to find and retrieve the resume data with
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"packages/*"
],
"engines": {
"node": ">=18.0.0"
"node": ">=18.17.1 <19.0.0"
},
"browser": {
"fs": false,
Expand Down
1 change: 1 addition & 0 deletions packages/@aetherspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"axios": "^1.4.0",
"graphql": "^16.7.1",
"graphql-request": "^6.1.0",
"graphql-type-json": "^0.3.2",
"swr": "^2.2.0",
"twrnc": "^3.6.1",
"zod": "~3.20.6"
Expand Down
60 changes: 48 additions & 12 deletions packages/@aetherspace/schemas/aetherGraphSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { gql } from '@apollo/client'
import { GraphQLJSON, GraphQLJSONObject } from 'graphql-type-json'
import { aetherSchemaPlugin } from './aetherSchemaPlugin'
import { AetherSchemaType } from './aetherSchemas'
import { isEmpty } from '../utils/commonUtils'

/* --- Scalars --------------------------------------------------------------------------------- */

const CUSTOM_SCALARS = ['scalar Date']
const CUSTOM_SCALARS = ['scalar Date', 'scalar JSON', 'scalar JSONObject']

/* --- Types ----------------------------------------------------------------------------------- */

Expand Down Expand Up @@ -123,8 +125,8 @@ const aetherSchemaDefinitions = (aetherSchema: ResolverSchemaType, prefix = 'typ
// -- Arraylikes --
AetherArray: createDefinition('Array'),
// -- Complex types --
// AetherUnion: createDefinition('Union') // TODO: To implement
// AetherTuple: createDefinition('Tuple') // TODO: To implement
AetherUnion: createDefinition('JSON'),
AetherTuple: createDefinition('JSON'),
})
// Transform into usable graphql definitions
const schemaDef = `
Expand Down Expand Up @@ -178,7 +180,24 @@ const createResolverDefinition = (resolverConfig: ResolverConfigType) => {

/** --- aetherGraphSchema() -------------------------------------------------------------------- */
/** -i- Turn a mapped object of aetherResolvers into an executable GraphQL schema */
const aetherGraphSchema = (aetherResolvers: ResolverMapType) => {
const aetherGraphSchema = (
aetherResolvers: ResolverMapType,
{
customSchemaDefinitions = '',
customQueryDefinitions = '',
customMutationDefinitions = '',
customScalars = {},
customQueries = {},
customMutations = {},
}: {
customSchemaDefinitions?: string
customQueryDefinitions?: string
customMutationDefinitions?: string
customScalars?: Record<string, (...args: unknown[]) => Promise<unknown>>
customQueries?: Record<string, (...args: unknown[]) => Promise<unknown>>
customMutations?: Record<string, (...args: unknown[]) => Promise<unknown>>
} = {}
) => {
const resolverEntries = Object.entries(aetherResolvers)
const resolverConfigs = resolverEntries.map(([resolverName, resolver]) => ({
resolverName,
Expand All @@ -187,27 +206,44 @@ const aetherGraphSchema = (aetherResolvers: ResolverMapType) => {
isMutation: !!resolver?.isMutation,
resolver,
}))

const mutationConfigs = resolverConfigs.filter((resolverConfig) => resolverConfig.isMutation)
const mutationDefs = [customMutationDefinitions, ...mutationConfigs.map(createResolverDefinition)].filter(Boolean) // prettier-ignore
const hasMutations = mutationDefs.length > 0 || !isEmpty(customMutations)

const queryConfigs = resolverConfigs.filter((resolverConfig) => !resolverConfig.isMutation)
const dataTypeDefs = Array.from(new Set(aetherGraphDefinitions(resolverConfigs)))
const mutationDefs = mutationConfigs.map(createResolverDefinition)
const queryDefs = queryConfigs.map(createResolverDefinition)
const hasMutations = mutationDefs.length > 0
const hasQueries = queryDefs.length > 0
const queryDefs = [customQueryDefinitions, ...queryConfigs.map(createResolverDefinition)].filter(Boolean) // prettier-ignore
const hasQueries = queryDefs.length > 0 || !isEmpty(customQueries)

const mutation = hasMutations ? `type Mutation {\n ${mutationDefs.join('\n ')}\n}` : ''
const query = hasQueries ? `type Query {\n ${queryDefs.join('\n ')}\n}` : ''
const allTypeDefs = [...CUSTOM_SCALARS, ...dataTypeDefs, mutation, query].filter(Boolean)

const dataTypeDefs = Array.from(new Set(aetherGraphDefinitions(resolverConfigs)))
const allTypeDefs = [
customSchemaDefinitions,
...CUSTOM_SCALARS,
...dataTypeDefs,
mutation,
query,
].filter(Boolean)

const typeDefsString = allTypeDefs.join('\n\n')
const graphqlSchemaDefs = gql`${typeDefsString}` // prettier-ignore

const rebuildFromConfig = (handlers, { resolverName, resolver }) => ({
...handlers,
[resolverName]: resolver,
})
const queryResolvers = queryConfigs.reduce(rebuildFromConfig, {})
const mutationResolvers = mutationConfigs.reduce(rebuildFromConfig, {})

const queryResolvers = queryConfigs.reduce(rebuildFromConfig, customQueries)
const mutationResolvers = mutationConfigs.reduce(rebuildFromConfig, customMutations)

return {
typeDefs: graphqlSchemaDefs,
resolvers: {
JSON: GraphQLJSON,
JSONObject: GraphQLJSONObject,
...customScalars,
...(hasQueries ? { Query: queryResolvers } : {}),
...(hasMutations ? { Mutations: mutationResolvers } : {}),
},
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10760,6 +10760,11 @@ graphql-tag@^2.10.1, graphql-tag@^2.12.6:
dependencies:
tslib "^2.1.0"

graphql-type-json@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.3.2.tgz#f53a851dbfe07bd1c8157d24150064baab41e115"
integrity sha512-J+vjof74oMlCWXSvt0DOf2APEdZOCdubEvGDUAlqH//VBYcOYsGgRW7Xzorr44LvkjiuvecWc8fChxuZZbChtg==

[email protected]:
version "15.8.0"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.8.0.tgz#33410e96b012fa3bdb1091cc99a94769db212b38"
Expand Down

1 comment on commit bead934

@vercel
Copy link

@vercel vercel bot commented on bead934 Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.