-
Notifications
You must be signed in to change notification settings - Fork 393
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
JSON type in GraphQL #111
Comments
@kamilmysliwiec please add an example of how to implement this package in code first way |
@kamilmysliwiec When i inject resolvers: { JSON: GraphQLJSON }, in the root config for the code first approach. It gives the Error: "JSON" defined in resolvers, but not in schema. how do we resolve this? |
any example how to use it with code first ? |
i am also struggling with the code-first approach and custom scalars. |
ok guys.. i managed to solve this issue. However, i was not able to directly include any of the existing packages (although this may be possible for sure). Here is, what i did: export class JSONObject {} Then, i created a new @Scalar('JSONObject', type => JSONObject)
export class JSONObjectScalar implements CustomScalar<object, object> {
description = 'JSONObject custom scalar type';
parseValue(value: object): object {
return value; // this is the value a client sends to the server
}
serialize(value: object): object {
return value; // this is the value the server sends to the client
}
parseLiteral(ast: any): object {
if (ast.kind === Kind.OBJECT) {
return new Object(ast.value);
}
return null;
}
} Then i "registered" this @Module({
providers: [
FooResolver,
// ... other providers like services and stuff
JSONObjectScalar
],
})
export class FooModule {} and finally use the newly defined import { Field, ID, ObjectType } from 'type-graphql';
import { JSONObjectScalar, JSONObject } from 'src/common/scalars/jsonobject.scalar';
@ObjectType()
export class Foo {
@Field(type => ID)
id: string;
// ... some properties here
@Field(type => JSONObject, { ... })
payload: object
} I really hope, this helps someone. All the best |
Here what I did to simply output JSON in an ObjectType: import { Field, ObjectType } from 'type-graphql';
import graphqlTypeJson from 'graphql-type-json';
@ObjectType()
export class JsonTest {
@Field(() => graphqlTypeJson, { nullable: true })
public result?: any;
} For my use case all is working fine. |
I also have that problem, could you solve it? |
JSON type for the code first approach. This works in my environment,
import graphqlTypeJson from 'graphql-type-json'
import { Field, ObjectType } from '@nestjs/graphql'
@ObjectType()
export class SomeClass {
@Field(() => graphqlTypeJson, { nullable: true })
json?: object
} Then you see the following in your
|
Works with also import { GraphQLJSONObject } from 'graphql-type-json';
@ObjectType()
export class SomeClass {
@Field(() => GraphQLJSONObject, { nullable: true })
json?: JSON;
} |
I'm submitting a...
Current behavior
https://www.apollographql.com/docs/graphql-tools/scalars.html
Is it possible to implement JSON as a GraphQL type using graphql-type-json or any other package or way? Sorry I haven't found any thread or examples on this, so wondering if its possible to do in nest like its done in Express
My GraphQL Module
Is it possible to include the type resolvers here ?
The text was updated successfully, but these errors were encountered: