-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add Relay-compatible GraphQL endpoint #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
google-labs-jules
wants to merge
10
commits into
master
Choose a base branch
from
feat/add-graphql-endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ba5aea
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] 536c8d6
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] f7cdef8
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] 2be608b
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] 432bb12
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] 4408e9a
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] dbb2148
Merge branch 'master' into feat/add-graphql-endpoint
hawkrives 927757f
reformat
abca58d
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] d65c9dd
feat: Add Relay-compatible GraphQL endpoint
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import {createHandler} from 'graphql-http/lib/use/koa' | ||
| import {GraphQLID, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString} from 'graphql' | ||
| import { | ||
| type ConnectionArguments, | ||
| connectionArgs, | ||
| connectionDefinitions, | ||
| connectionFromArray, | ||
| fromGlobalId, | ||
| nodeDefinitions, | ||
| toGlobalId, | ||
| } from 'graphql-relay' | ||
| import {get} from '../ccc-lib/http.js' | ||
| import {GH_PAGES} from '../ccci-stolaf-college/v1/gh-pages.js' | ||
| import DataLoader from 'dataloader' | ||
| import {z, type ZodObject, type ZodRawShape, type ZodTypeAny} from 'zod' | ||
| import {URLScalar} from './url-scalar.js' | ||
|
|
||
| // #region Type Definitions and Zod Schemas | ||
| // ================================================================================= | ||
|
|
||
| type KnownGraphQLTypeNames = 'Contact' | 'DictionaryDefinition' | ||
|
|
||
| const ContactSchema = z.object({ | ||
| title: z.string(), | ||
| phoneNumber: z.string(), | ||
| buttonText: z.string(), | ||
| category: z.string(), | ||
| image: z.string().optional(), | ||
| synopsis: z.string(), | ||
| text: z.string(), | ||
| }) | ||
| type Contact = z.infer<typeof ContactSchema> | ||
|
|
||
| interface ContactResponse { | ||
| data: Contact[] | ||
| } | ||
|
|
||
| const DictionaryDefinitionSchema = z.object({ | ||
| word: z.string(), | ||
| definition: z.string(), | ||
| }) | ||
|
|
||
| type DictionaryDefinition = z.infer<typeof DictionaryDefinitionSchema> | ||
|
|
||
| interface DictionaryDefinitionResponse { | ||
| data: DictionaryDefinition[] | ||
| } | ||
|
|
||
| // #endregion | ||
|
|
||
| // #region Data Loaders | ||
| // ================================================================================= | ||
|
|
||
| const contactLoader = new DataLoader<string, Contact[]>(async (keys) => { | ||
| const contacts = await Promise.all(keys.map((key) => get(GH_PAGES(key)).json<ContactResponse>())) | ||
| return contacts.map((contact) => contact.data) | ||
| }) | ||
|
|
||
| const dictionaryLoader = new DataLoader<string, DictionaryDefinition[]>(async (keys) => { | ||
| const dictionary = await Promise.all( | ||
| keys.map((key) => get(GH_PAGES(key)).json<DictionaryDefinitionResponse>()), | ||
| ) | ||
| return dictionary.map((entry) => entry.data) | ||
| }) | ||
|
|
||
| // #endregion | ||
|
|
||
| // #region Node Interface and Type Registry | ||
| // ================================================================================= | ||
|
|
||
| interface NodeTypeInfo<T extends ZodTypeAny> { | ||
| schema: T | ||
| fetcher: (id: string) => Promise<z.infer<T> | null> | ||
| } | ||
|
|
||
| const typeInfoRegistry: { | ||
| [key in KnownGraphQLTypeNames]?: NodeTypeInfo<ZodObject<ZodRawShape>> | ||
| } = {} | ||
|
|
||
| function registerType<T extends ZodObject<ZodRawShape>>( | ||
| name: KnownGraphQLTypeNames, | ||
| info: NodeTypeInfo<T>, | ||
| ) { | ||
| typeInfoRegistry[name] = info | ||
| } | ||
|
|
||
| registerType('Contact', { | ||
| schema: ContactSchema, | ||
| fetcher: async (id: string) => { | ||
| const contacts = await contactLoader.load('contact-info.json') | ||
| return contacts.find((c) => c.title === id) ?? null | ||
| }, | ||
| }) | ||
|
|
||
| registerType('DictionaryDefinition', { | ||
| schema: DictionaryDefinitionSchema, | ||
| fetcher: async (id: string) => { | ||
| const definitions = await dictionaryLoader.load('dictionary.json') | ||
| return definitions.find((d) => d.word === id) ?? null | ||
| }, | ||
| }) | ||
|
|
||
| function getObject(globalId: string): Promise<object | null> { | ||
| const {type, id} = fromGlobalId(globalId) | ||
| const info = typeInfoRegistry[type as KnownGraphQLTypeNames] | ||
| if (info) { | ||
| return info.fetcher(id) | ||
| } | ||
| return Promise.resolve(null) | ||
| } | ||
|
|
||
| function resolveNodeType(obj: object): KnownGraphQLTypeNames | undefined { | ||
| for (const name in typeInfoRegistry) { | ||
| if (Object.prototype.hasOwnProperty.call(typeInfoRegistry, name)) { | ||
| const info = typeInfoRegistry[name as KnownGraphQLTypeNames] | ||
| if (info?.schema.safeParse(obj).success) { | ||
| return name as KnownGraphQLTypeNames | ||
| } | ||
| } | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| const {nodeInterface, nodeField} = nodeDefinitions(getObject, resolveNodeType) | ||
|
|
||
| // #endregion | ||
|
|
||
| // #region GraphQL Object Types | ||
| // ================================================================================= | ||
|
|
||
| const ContactType = new GraphQLObjectType<Contact, unknown>({ | ||
| name: 'Contact', | ||
| fields: { | ||
| id: { | ||
| type: new GraphQLNonNull(GraphQLID), | ||
| resolve: (contact) => toGlobalId('Contact', contact.title), | ||
| }, | ||
| title: {type: new GraphQLNonNull(GraphQLString)}, | ||
| phoneNumber: {type: new GraphQLNonNull(GraphQLString)}, | ||
| buttonText: {type: new GraphQLNonNull(GraphQLString)}, | ||
| category: {type: new GraphQLNonNull(GraphQLString)}, | ||
| image: { | ||
| type: URLScalar, | ||
| resolve: (contact) => { | ||
| if (!contact.image) { | ||
| return null | ||
| } | ||
| try { | ||
| return new URL(contact.image) | ||
| } catch { | ||
| return null | ||
| } | ||
| }, | ||
| }, | ||
| synopsis: {type: new GraphQLNonNull(GraphQLString)}, | ||
| text: {type: new GraphQLNonNull(GraphQLString)}, | ||
| }, | ||
| interfaces: [nodeInterface], | ||
| }) | ||
|
|
||
| const DictionaryDefinitionType = new GraphQLObjectType<DictionaryDefinition, unknown>({ | ||
| name: 'DictionaryDefinition', | ||
| fields: { | ||
| id: { | ||
| type: new GraphQLNonNull(GraphQLID), | ||
| resolve: (def) => toGlobalId('DictionaryDefinition', def.word), | ||
| }, | ||
| word: {type: new GraphQLNonNull(GraphQLString)}, | ||
| definition: {type: new GraphQLNonNull(GraphQLString)}, | ||
| }, | ||
| interfaces: [nodeInterface], | ||
| }) | ||
|
|
||
| const {connectionType: ContactConnection} = connectionDefinitions({ | ||
| name: 'Contact', | ||
| nodeType: ContactType, | ||
| }) | ||
|
|
||
| const {connectionType: DictionaryDefinitionConnection} = connectionDefinitions({ | ||
| name: 'DictionaryDefinition', | ||
| nodeType: DictionaryDefinitionType, | ||
| }) | ||
|
|
||
| // #endregion | ||
|
|
||
| // #region Root Query and Schema | ||
| // ================================================================================= | ||
|
|
||
| const schema = new GraphQLSchema({ | ||
| query: new GraphQLObjectType({ | ||
| name: 'Query', | ||
| fields: { | ||
| node: nodeField, | ||
| hello: { | ||
| type: GraphQLString, | ||
| resolve: () => 'world', | ||
| }, | ||
| contacts: { | ||
| type: ContactConnection, | ||
| args: connectionArgs, | ||
| resolve: async (_, args: ConnectionArguments) => { | ||
| const contacts = await contactLoader.load('contact-info.json') | ||
| return connectionFromArray(contacts, args) | ||
| }, | ||
| }, | ||
| dictionary: { | ||
| type: DictionaryDefinitionConnection, | ||
| args: connectionArgs, | ||
| resolve: async (_, args: ConnectionArguments) => { | ||
| const definitions = await dictionaryLoader.load('dictionary.json') | ||
| return connectionFromArray(definitions, args) | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| }) | ||
|
|
||
| export const graphql = createHandler({schema}) | ||
|
|
||
| // #endregion | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think we extracted other schemas to their own files but should be okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figure we can figure out the desired file structure for these later, but … eh, lemme split them out now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine, we're making gradual changes and can polish later