Replies: 1 comment
-
I extended the SDLConverter class and adapted it to fit my use-case. I can now optionally pass a It's pretty raw for now but it works, I turned it into a graphql-codegen plugin so I can produce nexus types whenever there is a such in my upstream graphql schema (Hasura). import { isSpecifiedScalarType } from "graphql";
import { SDLConverter } from "nexus/dist/sdlConverter";
interface ConvertSDLOptions {
commonjs?: boolean;
json?: JSON;
scalarsPath?: string;
}
export class CodegenSDLConverter extends SDLConverter {
protected scalarsPath: string;
constructor(sdl: string, options?: ConvertSDLOptions) {
super(sdl, options?.commonjs, options?.json);
this.scalarsPath = options?.scalarsPath ?? "./scalars";
}
print() {
const body = [
this.printObjectTypes(),
this.printInterfaceTypes(),
this.printInputObjectTypes(),
this.printUnionTypes(),
this.printEnumTypes(),
this.printExports(),
]
return [this.printUsedImports(), this.printUsedScalarImports()]
.concat(body)
.filter((f) => f)
.join('\n\n')
}
printUsedScalarImports() {
if (!this.groupedTypes.scalar.length) {
return "";
}
const usedScalarImports = this.groupedTypes.scalar
.filter((s) => !isSpecifiedScalarType(s))
.map((t) => t.name);
if (this.commonjs) {
return `const { ${Array.from(usedScalarImports).join(', ')} } = require("${this.scalarsPath}");`
}
return `import { ${Array.from(usedScalarImports).join(', ')} } from "${this.scalarsPath}";`
}
}
export function convertSDL(sdl: string, options?: ConvertSDLOptions) {
try {
return new CodegenSDLConverter(sdl, options).print()
} catch (e) {
return `Error Parsing SDL into Schema: ${e.stack}`
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello all,
I'm starting playing with nexus, and I think I have a fun use-case.
We're using Hasura as our main GraphQL API, and Nexus seems the perfect candidate for implementing our business logic as a remote schema.
We're using TypeScript and code generation in anger, and want Hasura to be the single source of truth for our entire stack.
I was very happy to find that Nexus has a SDL converter and that I can use it to easily reuse all the types generated on Hasura's side. I made a tiny script to trigger the sdl conversion whenever my local
schema.graphql
changes and it works great :Now I noticed that the custom scalar types coming from Hasura (uuid, timestamptz) are available as :
So the SDL converter is more for "starting with Nexus" than "integrate nexus with another schema".
In my case I don't want to edit this file, I want to reuse parts of it (mostly types) in my custom queries and mutations.
It would be great to provide the convertSDL those scalars as parameter so I can have a predictable generated file and no need to edit it.
It would also be great to have more control over what we want to keep from the remote schema. For example I won't need any of the query or mutation fields, only the base types are useful in my situation. 😄
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions