Created
March 24, 2020 19:12
-
-
Save outerlook/c88a98335f976bba0a51db9de135f0c5 to your computer and use it in GitHub Desktop.
Nexus-Prisma Relay Spec compliant server implementation (Work in progress, not tested)
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
import { interfaceType, objectType } from "nexus"; | |
import { decode, encode } from "../../utils/relay-tools-custom"; | |
export const Node = interfaceType({ | |
name: "Node", | |
definition(t) { | |
t.resolveType(({ id }) => decode(id).__typename as any); | |
t.id("id", { | |
description: "CUID for a resource", | |
nullable: false, | |
resolve: ({ id }, args, ctx, { parentType }) => { | |
console.log(parentType); | |
return encode(id, parentType.name); | |
} | |
}); | |
} | |
}); | |
// node interface definition | |
export const Estado = objectType({ | |
name: "Estado", | |
definition: (t) => { | |
t.implements("Node"); | |
t.model.idIbge(); | |
t.model.nome(); | |
t.model.sigla(); | |
t.model.cidades(); | |
} | |
}); | |
// example object definition that implements interface |
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
export const Queries = queryType({ | |
definition: (t) => { | |
t.field("node", { | |
type: "Node", | |
args: { id: idArg({ required: true }) }, | |
resolve: (root, args, context, info) => { | |
const { id, __typename } = decode(args.id); | |
const objeto = __typename.charAt(0).toLowerCase() + __typename.slice(1); // from TitleCase to camelCase | |
return { | |
...context.prisma[objeto].findOne({ where: { id } }), | |
__typename | |
}; | |
} | |
}); | |
t.field("horarioServidor", { type: "DateTime", resolve: () => new Date() }); | |
t.crud.categoriaDeComodidades(); | |
t.crud.estados(); | |
// ... continues | |
} | |
}); |
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
export const decode = (objectId: string) => { | |
const decoded = Buffer.from(objectId, "base64").toString("utf8"); | |
const parts = decoded.split(":"); | |
return { | |
id: parts[0], | |
__typename: parts[1] | |
}; | |
}; | |
export const encode = (id: string, __typename: string) => | |
Buffer.from(`${id}:${__typename}`, "utf8").toString("base64"); | |
export const fromGlobalId = (globalId: string) => { | |
return decode(globalId).id; | |
}; | |
export const toGlobalId = ({ | |
id, | |
__typename | |
}: { | |
id: string; | |
__typename: string; | |
}) => encode(id, __typename); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment