graphile / crystal

🔮 Graphile's Crystal Monorepo; home to Grafast, PostGraphile, pg-introspection, pg-sql2 and much more!
https://graphile.org/
Other
12.63k stars 572 forks source link

Customising GraphQL names for types/collection fields/etc #626

Closed Tritpi closed 6 years ago

Tritpi commented 7 years ago

Is there a way to customize the GraphQL generated schema so that the way in which the tables can be accessed can be changed. Currently, in the GraphiQL interface the table names of tables in the underlying Postgres DB get the word 'all' prefixed. Can this be customized such that the user gets to access the table name exactly the way it is in the DB. Also, can the schema be modified such that the nodes,edges,totalcount etc. can be taken off or renamed.

We are aware that the auto-export feature can be used to export the GraphQL generated schema locally. But how can this be used to tweak the schema or customize it according to our requirements.

baohuynh27 commented 7 years ago

Yes

5

Vào 16-11-2017 14:04, "Tritpi" notifications@github.com đã viết:

Is there a way to customize the GraphQL generated schema so that the way in which the tables can be accessed can be changed. Currently, in the GraphiQL interface the table names of tables in the underlying Postgres DB get the word 'all' prefixed. Can this be customized such that the user gets to access the table name exactly the way it is in the DB. Also, can the schema be modified such that the nodes,edges,totalcount etc. can be taken off or renamed.

We are aware that the auto-export feature can be used to export the GraphQL generated schema locally. But how can this be used to tweak the schema or customize it according to our requirements.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/postgraphql/postgraphql/issues/626, or mute the thread https://github.com/notifications/unsubscribe-auth/AfZ33C6fPxdYibwD0O1ueuDnQO1f0Rffks5s295hgaJpZM4QgDAu .

Tritpi commented 7 years ago

Can you please elaborate on how this can be done with an example.

benjie commented 7 years ago

Check out the inflector in v4:

https://github.com/postgraphql/postgraphql/pull/506#issuecomment-328831456

For the other fields check out our extending docs:

https://www.graphile.org/postgraphile/extending/

disarticulate commented 6 years ago

ran into this issue also. It just seems too opinionated...

particularly when you have a table like plan_index

and the generated query for it is allPlanIndices

That's certainly human, but it's a road block for dynamic table creation. the prefix all is ok, but why do you need to change the plurals? and further, why change such an oddball plural?

I think the appropriate solution would be to create aliases.

benjie commented 6 years ago

At the moment you can override the inflector if you don’t like its defaults. In future you’ll be able to specify per-table overrides using smart comments.

disarticulate commented 6 years ago

I'm running it inside a container, so it's easier to just look at what the default dependencies are than try to override anything. luckily, i'm building the frontend in javascript, so it's not difficult.

benjie commented 6 years ago

You can provide custom inflectors quite easily in v4. In this case we can override the allRows method to trim the all- prefix:

MyInflectorPlugin.js:

const { makeAddInflectorsPlugin } = require('graphile-utils');

module.exports = makeAddInflectorsPlugin({
  allRows(table: PgClass) {
    return this.camelCase(
      // Was: `all-${this.pluralize(this._singularizedTableName(table))}`
      // Now:
      this.pluralize(this._singularizedTableName(table))
    );
  },
});

Then --append-plugins `pwd`/MyInflectorPlugin.js


The built in PG inflectors are defined in makeNewBuild and PgBasicsPlugin; you can overwrite any that you want. Be sure to not use arrow functions otherwise you'll override this and lose access to the other inflectors.

https://github.com/graphile/graphile-build/blob/0fce134cac219d196ec97ead668ddbef7140b66c/packages/graphile-build/src/makeNewBuild.js#L760-L766

https://github.com/graphile/graphile-build/blob/0fce134cac219d196ec97ead668ddbef7140b66c/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js#L161-L542

Note also that smart comment renaming is now available: https://www.graphile.org/postgraphile/smart-comments/#renaming