Skip to content
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

allow customising graphql schema for a table (close #981) #2509

Merged
merged 28 commits into from
Sep 19, 2019

Conversation

rakeshkky
Copy link
Member

@rakeshkky rakeshkky commented Jul 11, 2019

Requires merging hasura/graphql-parser-hs#17

TODO:-

  • Add tests
  • Add docs
  • Console integration

Description

Allow users to customize root field names of a Postgres table and column fields in GraphQL schema. However, original Postgres column names are used to define metadata such as relationships, permissions etc.

Default root field names generated for a table author are:

Queries:-

  • author
  • author_by_pk
  • author_aggregate

Mutations:-

  • insert_author
  • delete_author
  • update_author

TODO:- insert UI screenshots after console work is done.

Note:- Only root field names and columns are customizable as of now. I'm open to any request for customizing other parts of Hasura generated GraphQL schema

Affected components

  • Server
  • Console
  • Docs
  • Tests

Related Issues

#981

Solution and Design

A new optional version field whose type is Int is introduced in /v1/query payload which specifies
the version of the query. If absent version is considered as 1. All existing query types come under version: 1

track_table query type is supported in version: 2.

Example:-

POST /v1/query

{
    "type": "track_table",
    "version": 2,
    "args": {
        "table": "user",
        "configuration": {
            "custom_root_fields": {
                "select": "Users",
                "select_by_pk": "User",
                "select_aggregate": "UserAggregate",
                "insert": "AddUsers",
                "update": "UpdateUsers",
                "delete": "DeleteUsers"
            },
            "custom_column_names": {
            	"id": "userId"
            }
        }
    }
}

A new set_table_custom_fields query type is added in version: 2. It is used to update the custom field names of an already tracked table.

Example:-

POST /v1/query

{
  "type": "set_table_custom_fields",
  "version": 2,
  "args": {
    "table": "author",
    "custom_root_fields": {
      "select": "Authors",
      "select_by_pk": "Author"
    },
    "custom_column_names": {
      "id": "AuthorId",
      "name": "AuthorName"
    }
  }
}

Catalog changes:-

A new nullable column configuration of type JSONB is added to table
hdb_catalog.hdb_table which stores the configuration JSON object found in track_table v2 query.

For Console:

Refer docs to learn more about new track_table version 2 API.

NOTE: Tracking an already tracked table using version 2 track_table API won't throw API exception, instead it'll update the configuration in metadata cache and catalog.

Steps to test and verify

Limitations, known bugs & workarounds

@rakeshkky rakeshkky added s/do-not-merge Do not merge this pull request to master c/console Related to console c/server Related to server c/docs Related to docs labels Jul 11, 2019
@rakeshkky rakeshkky requested a review from 0x777 as a code owner July 11, 2019 07:39
@rakeshkky rakeshkky self-assigned this Jul 11, 2019
@netlify
Copy link

netlify bot commented Jul 11, 2019

Deploy preview for hasura-docs ready!

Built with commit 6309ce5

https://deploy-preview-2509--hasura-docs.netlify.com

@hasura-bot
Copy link
Contributor

Review app for commit a19496d deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-a19496db

@ecthiender
Copy link
Contributor

@rakeshkky @0x777 why aren't we doing /v2/query instead of the version key in the payload? It seems to defeat the purpose of having a versioned endpoint.

@0x777
Copy link
Member

0x777 commented Jul 11, 2019

@ecthiender If you have a separate endpoint /v2/query you won't be able to mix v1 and v2 queries in a single bulk query. Also the version in /v1/query would change if something fundamental changes, like the structure changes from type and args and also version key can be introduced alongside type, args in a backwards compatible way.

Resolve Conflicts:
	server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/RQL/Types/Common.hs
@rakeshkky
Copy link
Member Author

As of now, the only way to update configuration of a table is to untrack and track table with the updated configuration. We lose all permissions and relationships of the table.

I have a plan to add update_table_configuration query type which will update the configuration of a table. If I add, what should be the version of that query?

@0x777 @ecthiender @shahidhk thoughts?

@hasura-bot
Copy link
Contributor

Review app for commit 48032f7 deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-48032f7f

@shahidhk shahidhk changed the title allow customising GraphQL root fields of a Table (close #981) Allow customising GraphQL root fields of a Table (close #981) Jul 15, 2019
@shahidhk
Copy link
Member

I have a plan to add update_table_configuration query type which will update the configuration of a table. If I add, what should be the version of that query?

It would be version nil (or 1) ideally, but if the code is easier managed if the version is 2, go ahead with 2.

@rakeshkky rakeshkky changed the title Allow customising GraphQL root fields of a Table (close #981) add customising GraphQL root fields of a Table (close #981) Jul 23, 2019
@rakeshkky rakeshkky changed the title add customising GraphQL root fields of a Table (close #981) allow customising GraphQL root fields of a Table (close #981) Jul 23, 2019
Resolve Conflicts:
	server/src-exec/Migrate.hs
	server/src-lib/Hasura/RQL/Types/SchemaCache.hs
	server/src-lib/Hasura/Server/App.hs
	server/src-lib/Hasura/Server/Query.hs
@hasura-bot
Copy link
Contributor

Review app for commit e0a5f4b deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-e0a5f4bf

@hasura-bot
Copy link
Contributor

Review app for commit c8e1452 deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-c8e14524

@hasura-bot
Copy link
Contributor

Review app for commit 6b00fec deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-6b00fecf

Resolve Conflicts:
	server/src-lib/Hasura/Server/Utils.hs
@hasura-bot
Copy link
Contributor

Review app for commit 7ffb524 deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-7ffb5246

@hasura-bot
Copy link
Contributor

Review app for commit 75f250e deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-75f250ee

@rakeshkky rakeshkky force-pushed the issue-981-custom-graphql branch from dc67263 to 5973e2f Compare September 13, 2019 12:52
@hasura-bot
Copy link
Contributor

Review app for commit 5973e2f deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-5973e2f9

Resolve Conflicts:
	server/src-exec/Migrate.hs
	server/src-lib/Hasura/GraphQL/Resolve/Insert.hs
	server/src-lib/Hasura/GraphQL/Resolve/Mutation.hs
	server/src-lib/Hasura/GraphQL/Resolve/Select.hs
	server/src-lib/Hasura/GraphQL/Schema.hs
	server/src-lib/Hasura/GraphQL/Schema/BoolExp.hs
	server/src-lib/Hasura/GraphQL/Schema/Common.hs
	server/src-lib/Hasura/GraphQL/Schema/Mutation/Common.hs
	server/src-lib/Hasura/GraphQL/Schema/Select.hs
	server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/RQL/DML/Internal.hs
	server/src-lib/Hasura/RQL/DML/Returning.hs
	server/src-lib/Hasura/RQL/GBoolExp.hs
	server/src-lib/Hasura/RQL/Types/Column.hs
@rakeshkky
Copy link
Member Author

@lexi-lambda Please have a final glance. Suggest the changes if any.

@hasura-bot
Copy link
Contributor

Review app for commit 6309ce5 deployed to Heroku: https://hge-ci-pull-2509.herokuapp.com
Docker image for server: hasura/graphql-engine:pull2509-6309ce5d

@shahidhk shahidhk changed the title allow customising GraphQL root fields of a Table (close #981) allow customising graphql root fields of a table (close #981) Sep 19, 2019
@shahidhk shahidhk changed the title allow customising graphql root fields of a table (close #981) allow customising graphql schema generated for a table (close #981) Sep 19, 2019
Copy link
Member

@shahidhk shahidhk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shahidhk
Copy link
Member

I am guessing console changes are going out as a separate PR

@shahidhk shahidhk changed the title allow customising graphql schema generated for a table (close #981) allow customising graphql schema for a table (close #981) Sep 19, 2019
@shahidhk shahidhk merged commit 9bd5826 into hasura:master Sep 19, 2019
@hasura-bot
Copy link
Contributor

Review app https://hge-ci-pull-2509.herokuapp.com is deleted

@dsandip dsandip removed the s/do-not-merge Do not merge this pull request to master label Sep 20, 2019
polRk pushed a commit to polRk/graphql-engine that referenced this pull request Feb 12, 2020
…ra#2509)

* allow customizing GraphQL root field names, close hasura#981

* document v2 track_table API in reference

* support customising column field names in GraphQL schema

* [docs] add custom column fields doc in API reference

* add tests

* rename 'ColField' to 'ColumnField'

* embed column's graphql field in 'PGColumnInfo'

-> Value constructor of 'PGCol' is not exposed
-> Using 'parseJSON' to construct 'PGCol' in 'FromJSON' instances

* avoid using 'Maybe TableConfig'

* refactors & 'custom_column_fields' -> 'custom_column_names'

* cli-test: add configuration field in metadata export test

* update expected keys in `FromJSON` instance of `TableMeta`

* use `buildSchemaCacheFor` to update configuration in v2 track_table

* remove 'GraphQLName' type and use 'isValidName' exposed from parser lib

* point graphql-parser-hs library git repo to hasura

* support 'set_table_custom_fields' query API & added docs and tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/docs Related to docs c/server Related to server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants