Comments (15)
@fosk06 For error handling you are not required to do something like unions. In your app.module or graphql.module you can simply just do this
.apply(
graphqlExpress(async req => ({
schema: schema
rootValue: req,
context: req,
formatError: (error: GraphQLError) => {
return error.originalError instanceof BaseException ? error.originalError.serialize() : error;
}
}))
)
@kamilmysliwiec But for real unions and interfaces it is not quite clear how to resolve those.
# Schema
union SearchResult = Human | Droid | Starship
# Query
{
search(text: "an") {
... on Human {
name
height
}
... on Droid {
name
primaryFunction
}
... on Starship {
name
length
}
}
}
A NestJS way could look somethin like
@ResolveType('SearchResult')
async resolveType(obj) {
if(obj.height){
return 'Human';
}
if(obj.length){
return 'Starship';
}
}
What do you think?
from graphql.
Hey guys @kamilmysliwiec @ph55 @obedm503
I faced a similar problem but a bit different:
Let's say we have the following schema:
type Component {
id: ID!
type: ComponentTypes!
order: Int!
properties: ComponentProps!
}
enum ComponentTypes {
TEXT
BUTTON
}
union ComponentProps = TextComponentProps | ButtonComponentProps
type TextComponentProps {
text: String
}
type ButtonComponentProps {
color: String
}
How to resolve union ComponentProps
as it depend on Component.type
value?
from graphql.
Actually, this should be enough (you can omit __resolveType
inside parenthesis):
@Resolver('LoginResponse')
export class LoginResponseResolver {
@ResolveProperty()
__resolveType(obj) {
if (obj.error) {
return 'LoginFailure';
}
return 'LoginSuccess';
}
}
from graphql.
you can do this by defining it as a normal type
@Resolver('LoginResponse')
export class LoginResponseResolver {
@ResolveProperty('__resolveType')
__resolveType(obj) {
if (obj.error) {
return 'LoginFailure';
}
return 'LoginSuccess';
}
}
from graphql.
Anyone help me to demo union with nestjs + graphql ?
from graphql.
This is something I am struggling with as well.
from graphql.
Same here.
There is workaround though - add it manually when creating schema (ugly one)
const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphqls');
const resolvers = {
LoginResponse: {
__resolveType(obj, context, info) {
if (obj.error) {
return 'LoginFailure';
}
return 'LoginSuccess';
}
}
};
const schema = this.graphQLFactory.createSchema({ typeDefs, resolvers });
from graphql.
Yay, thanks
from graphql.
Hey @kamilmysliwiec @ph55 @obedm503
Can you help with some advice on this ⬆️ please
from graphql.
Still unclear on how this is meant to work. @kamilmysliwiec, would you mind clarifying this use case? I have a resolver the way you have specified and yet still receive the warning.
from graphql.
@igat64 it's not possible since TextComponentProps
or ButtonComponentProps
doesn't have a way to relate back to the component type. the code would just have to know the relationships
from graphql.
Thank you for the answer @obedm503
from graphql.
Hey, great solution but i think it could be better, just using a ResolveProperty function instead using a Resolver, because it needs the creation a new class and file for each union type, it creates multiples scopes of one model that should be summed up in one general resolver file. What do you guys think about it?
from graphql.
@yasserN What is your solution proposal?
from graphql.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from graphql.
Related Issues (20)
- definitions with option skipResolverArgs: true makes resolvers optional in d.ts HOT 5
- Using multiple `@ArgsType()` classes combines those arguments into one object and assigns it to all parameters which are an `@ArgsType` HOT 1
- Support for namespacing queries and mutations
- Always response Cannot return null for non-nullable field level2.level3 for nested resolver HOT 1
- GraphQL endpoints are not protected by the global Guards. HOT 2
- Nest can't resolve dependencies of the ApolloDriver. HOT 2
- TypeError: this.graphQlFactory.mergeWithSchema is not a function HOT 1
- If the sortSchema option is true, you want to sort the types, but not the fields. HOT 1
- CLI doesn't register object union with null HOT 1
- Add jsdoc for `debug` and `introspection` in `GraphQLModule` HOT 5
- TypeScript definitions generated with wrong case HOT 2
- `GraphQLModule` should throw an error if schema files are not found HOT 2
- Expose GraphQLModule options and gqlSchemaHost to support Serverless
- How to send partial data with error object using @Context
- CustomScalar parseLiteral doesn't allow null return type HOT 2
- Unable to Specify forRoutes Argument When Assigning Middleware to '/graphql' Endpoint
- ResolveField shouldn't be called when the field already has a value HOT 1
- Support for Custom Fields Decorators in Mapped types
- Extend `dateScalarMode` and `numberScalarMode` to allow custom scalars to be used by default
- Subscription graphql this on resolve does not access services being injected in the constructor HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql.