Comments (10)
How about implementing the alias by reflection over the GraphQL schema?
Basically just copy everything but the name.
Since the schema and the types can easily be queried using__schema
and __types
it's easy and doesn't impact performance as the resolving functions are identical.
This feature could be included in the GraphQL-JS library seperate from PostGraphQL.
My idea is to implement a ES6 Weak Map so it's possible to work with the type/query-object directly without stringifying them. Probably a bad idea since Weak Maps can't easily be looped over. Probably better to use an Array.
So the dev needs to create empty Types/Queries (empty as in only name is set but no fields or resolvers), and then pass both source and target to the Weak Map that's argument of some function. This way it's possible to customize the new endpoint with virtual fields that don't require a trip to Postgres. The aliases can be kept in a different file from the generated schema as for easy SQL schema updates.
I'm unsure when this should happen. At Postgres reflection time, node.js server initalizing, sowhere in between?
Edit: I just noticed that it could also be possible to copy fields and methods from the referenced GraphQL objects directly within JavaScript without actually querying the GraphQL server.
from crystal.
I finding a better name for fields like postNodesByAuthorId
on the server is fine by me because I feel that's what a lot of people find issue with.
What do you think about using constraint names for these field names? So if the reference to person
from the author_id
column was named author
our forward query name could be simply author
and our reverse query name could be postNodesByAuthor
.
Making connections optional is something I've discussed with @rattrayalex in #16. I don't really like the idea because it duplicates a lot of logic, but if enough people articulate a want/need I'll reconsider. I really think solving the postNodesByAuthorId
naming solution is the best compromise.
from crystal.
Isn't this what you're looking for?
If so, then a simple JSON map passed in might be enough for us.
from crystal.
@burkhardr No, that's not what where are trying to achieve. We want to alias the Query-definitions, because the Relay-compliant schemas aren't that human friendly.
Example: Alias users[0].posts[0].name
means usersNodes[0].edges[0].node.postsNodesByUserId.edges[0].name
from crystal.
Well, you could do that server-side, but the GraphQL connections / edges are named like that for a reason. The link I posted above allows you to do aliasing on the client-side, which - while not as convenient - is definitely the easier solution.
from crystal.
This issue discusses a server side implementation. The thing with the alias in the query is that it's just working on one level.
With the built-in alias the only thing that actually happens is that the returned key has a different name, paths can't be simplified however. The primary function for alias is to be able to request and return multiple instances in one request.
By design this GraphQL query is impossible to fulfill:
{
User(id: 1) {
name,
age
},
User(id: 2) {
name,
email
}
}
Because it's impossible to return both objects on the same User
key.
The aim of the discussed alias function is to make querying nicer, the builtin function just complicates the query as it's longer:
{
User1: User(id: 1) {
name,
age
},
User2: User(id: 2) {
name,
email
}
}
from crystal.
If you think of GraphQL as a straight REST replacement, then data structures like users[0].posts[0].name
make perfect sense. Unfortunately, we're dealing with a graph here, so you won't be able to simplify things like edges and nodes away without losing some of the functionality they provide (e.g. pageInfo
).
If you're willing to sacrifice that for added simplicity, then this feature should be strictly opt-in.
from crystal.
I don't understand why the feature should be opt-in.
The aliases have to be defined manually and they don't break the generated schema as they only offer a reference to the generated schema.
Can you elaborate?
from crystal.
Connections themselves shouldn't be optional. Inferring the name of the connection from the FK/column name makes sense to me, but you'll still not arrive at something like users[0].posts[0].name
(you'll always have edges[]
, node
, etc. for connections).
Sorry for the confusion. My sole argument was around not being able to boil a graph down to the same format you'd get from a classical REST endpoint without sacrificing some of its functionality.
from crystal.
Closing in favor of a discussion in #42.
from crystal.
Related Issues (20)
- Postgraphile plugin connection filter export to schema functionality pull request
- Multiple pgServices yields 'Attempted to add a second codec named 'varchar[]''
- Polymorphic types non-nullable attributes not working
- Weird behavior when setting pgServices.name HOT 2
- `@ref` should allow specifying the `from` so single-table polymorphism can apply refs to a single type
- Raise a GraphQL.js PR to allow enum values to be a callback HOT 2
- Comments on computed column functions are not present in the resulting GraphQL schema HOT 3
- Functions that return a specific polymorphic type
- Extra metadata to handle `@ref` relations for non-nullable relations plugin HOT 1
- Ruru incorrectly templates array values as singular HOT 1
- Error: New error HOT 2
- Outdated `chalk` dependency breaks `Postgraphile` when running with `--disable-proto=delete` or using `Deno` HOT 3
- Stack traces are displayed in production and can't be disabled HOT 1
- Plans returning constant(true) throw a step mis-match error
- Enums not matched in postgraphile.tags.json5 HOT 4
- Forbid exploring steps' dependencies during planning HOT 4
- Error when trying to add a directive with any arguments via makeExtendSchemaPlugin HOT 3
- Ruru headers are not passed along with requests to `/graphql/stream` HOT 4
- Intended way to return a union like `union Result = User | UsernameTaken | ...` in v5? HOT 2
- Set bodySizeLimit for graphile v5 HOT 4
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 crystal.