Comments (2)
The @include
and @skip
behavior in Graphene closely follows what is defined in the GraphQL
Specification. Firstly, after parsing the query string, the entire parsed AST is validated during the validation phase. Evaluation of the directives happens after the validation, during execution. Therefore we cannot change the behavior to your request, as this would break the rules of the spec. The purpose of validation is to ensure the type integrity of each query. Therefore, it does not make any sense to query fields that may or may not exist on your query, or to design your schema that way.
GraphQL is a strongly-typed language, and the schema serves as a contract between the client and server. In order for the server to fulfill that contract, it needs to know exactly what fields are queryable and what types they return. If a field is not queryable, it should not be included in the schema to begin with, and the server should not return it as an available field. Therefore, from a design perspective, it makes sense for the server to return an error if a non-queryable field is included in a query, regardless of whether it is excluded using the @include
directive.
There's several ways to elegantly solve your issue using the tools GraphQL provides, under the premise that you can modify both APIs:
-
Mark your banana box field as nullable
This way, you could have it on each query without the resolver failing in case it is not present. Since you already know when it isnull
on the client-side, you wouldn't even need to add any additional checks. -
Use an Interface or Union
You could model your Store as an interface with two Implementations:
interface CommonBanana {
id: ID!
common fields
}
type Banana {
id: ID!
common fields
}
type BoxableBanana {
id: ID!
common fields
bananaBox: BananaBox
}
Type Query {
banana: [CommonBanana]
}
And then query it like this:
query bananas {
banana {
id
commonFields
... on BoxableBanana {
banana_box {
id
size
}
}
}
}
In any case, I'd strongly recommend exploring solutions such as Apollo Federation (implemented in graphene-federation
) to stitch together the two APIs and prevent issues like this from arising. It's important to note that having two almost identical but separate GraphQL APIs is not an issue that can be solved in Graphene, but rather it's a matter of design decisions in your architecture that may need to be reconsidered. If that's not possible, the only way to avoid defining the query for each case is to modify the query AST on each request in your specific GraphQL client. Other than that, using fragments makes your life easier as well in this case 🙂
I hope my suggestions could provide some help, please feel free let me know if you have further questions.
Cheers
from graphene.
Thank you for the quick reply! And thanks a bunch for the clear recommendations. I think this helps me on my way sufficiently. I will definitely look into Apollo Federation, I hadn't heard about that yet.
Regarding our design decisions: I agree that the situation I described is quite specific and it may not be exactly what Graphene is designed to solve, but there is no other straightforward way for us to tackle our use case without introducing a lot of redundancy in our codebase.
Thanks a lot once more!
from graphene.
Related Issues (20)
- Allow configuring schema.introspect
- Thanks for Thanks for GraphQL Python Roadmap HOT 1
- Leading white spaces in description casuing issues HOT 1
- `graphql_schema` introspection doesn't output docstrings descriptions
- InputObjectType causing encoding issues with JSONField HOT 1
- Provide a way to configure default timezone of DateTimes HOT 3
- Turning Non-null to is_null HOT 3
- Enum conversion to Graphene Enum fails if Enum name is "type" HOT 1
- Enum equality comparison is inconsistent
- http://www.graphene-python.org Has EXPIRED HOT 3
- Upload file HOT 2
- Unexpected argument in Pycharm 2023.2.1 HOT 1
- Default_value option in Arguments inside Mutation is not working as expected HOT 2
- Exception: NonNull could not have a mounted String() as inner type. Try with NonNull(String).
- Returning non fatal errors alongside data
- Conversion to snake_case fails on nested inputs
- Depth limit validator key error if fragment undefined
- Tests in `graphene/relay/tests/test_custom_global_id.py` fail due incompatibility with pytest 8.x HOT 1
- Enum.from_enum() fails with Django IntegerChoices
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 graphene.