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

postgres functions as custom queries (close #333) #1073

Merged
merged 66 commits into from
Jan 25, 2019

Conversation

rakeshkky
Copy link
Member

@rakeshkky rakeshkky commented Nov 20, 2018

Description

What component does this PR affect?

  • Server
  • Console
  • CLI
  • Docs
  • Community Content
  • Build System

Requires changes from other components? If yes, please mark the components:

  • Server
  • Console
  • CLI
  • Docs
  • Community Content
  • Build System

Related Issue

close #333

Solution and Design

For table article.

create table article (id serial primary key, title text not null, content text not null, author_id integer not null)

Define a function search_articles as following:-

      create function search_articles(search text)
      returns setof article as $$
          select *
          from article
          where
          title ilike ('%' || search || '%') or
          content ilike ('%' || search || '%')
      $$ language sql stable;

Then GraphQL Engine generates custom query field on search_articles :-

search_articles(
args: search_articles_args! 
where: artcle_bool_exp
order_by: [article_order_by!] 
limit: Int
offset: Int
): [article!]! 

input search_article_args {
search: String!
}

Sample GraphQL query:-

query search_articles {
  search_articles(
    args: {search: "Hasura"}
  ){
    id
    title
    content
    author_id
  }
}

Metadata API:-

Adding a function

   POST /v1/query HTTP/1.1
   Content-Type: application/json
   X-Hasura-Role: admin

   {
       "type": "track_function",
       "args": {
           "schema": "public",
           "name": "search_articles"
       }
   }

Removing a function

   POST /v1/query HTTP/1.1
   Content-Type: application/json
   X-Hasura-Role: admin

   {
       "type": "untrack_function",
       "args": {
           "schema": "public",
           "name": "search_articles"
       }
   }

Type

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Docs update
  • Community content

Checklist:

  • I have read the contributing guide and my code conforms to the guidelines.
  • This change requires a change in the documentation.
  • I have updated the documentation accordingly.
  • I have added required tests.

TODO:-
-> functions as mutations
-> permissions
-> tests
Resolve Conflicts:
	console/src/components/Services/Data/Metadata/Metadata.js
	server/src-exec/Ops.hs
	server/src-lib/Hasura/GraphQL/Resolve.hs
	server/src-lib/Hasura/GraphQL/Resolve/BoolExp.hs
	server/src-lib/Hasura/GraphQL/Resolve/Select.hs
	server/src-lib/Hasura/GraphQL/Schema.hs
	server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/SQL/DML.hs
	server/src-lib/Hasura/SQL/Types.hs
	server/src-lib/Hasura/Server/Query.hs
	server/tests-py/test_graphql_queries.py
Resolve Conflicts:
	server/src-lib/Hasura/GraphQL/Resolve/Mutation.hs
	server/src-lib/Hasura/GraphQL/Resolve/Select.hs
Resolve Conflicts:
	server/src-exec/Ops.hs
	server/src-lib/Hasura/GraphQL/Resolve/BoolExp.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/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/RQL/Types/SchemaCache.hs
@rakeshkky rakeshkky requested a review from 0x777 November 20, 2018 13:20
@hasura-bot
Copy link
Contributor

Review app available at: https://hge-ci-pull-1073.herokuapp.com

@hasura-bot
Copy link
Contributor

Review app available at: https://hge-ci-pull-1073.herokuapp.com

@shahidhk shahidhk added c/server Related to server c/docs Related to docs labels Nov 21, 2018
@revskill10
Copy link

How about sql function as mutation ?

@shahidhk
Copy link
Member

shahidhk commented Nov 23, 2018

@revskill10 We'll add them in the coming iterations.

Resolve Conflicts:
	server/src-exec/Ops.hs
	server/src-exec/TH.hs
	server/src-lib/Hasura/GraphQL/Resolve/Context.hs
	server/src-lib/Hasura/GraphQL/Schema.hs
	server/src-lib/Hasura/Prelude.hs
	server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/RQL/DML/Select.hs
	server/src-lib/Hasura/RQL/Types/SchemaCache.hs
	server/src-lib/Hasura/Server/App.hs
	server/src-lib/Hasura/Server/Query.hs
	server/src-rsr/hdb_metadata.yaml
	server/src-rsr/initialise.sql
@revskill10
Copy link

So as i see, there won't be a subscription for the same query with custom sql function ?
e.g

subscription search_articles {
  search_articles(
    args: {search: "Hasura"}
  ){
    id
    title
    content
    author_id
  }
}

@rakeshkky
Copy link
Member Author

@revskill10 Subscription allows custom queries as well.

@hasura-bot
Copy link
Contributor

Review app for commit ee4d7e3 deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-ee4d7e3

@jasonmorganson
Copy link

@rakeshkky this would be a really useful feature. Any idea on when this will land?

@shahidhk
Copy link
Member

@jasonmorganson This should take another week or two.

Resolve Conflicts:
	server/src-exec/Ops.hs
	server/src-exec/TH.hs
	server/src-lib/Hasura/GraphQL/Context.hs
	server/src-lib/Hasura/GraphQL/Resolve/Select.hs
	server/src-lib/Hasura/GraphQL/Schema.hs
	server/src-lib/Hasura/Prelude.hs
	server/src-lib/Hasura/RQL/DDL/Schema/Table.hs
	server/src-lib/Hasura/RQL/DML/Select.hs
	server/src-lib/Hasura/Server/Query.hs
	server/src-rsr/migrate_from_5_to_6.sql
@hasura-bot
Copy link
Contributor

Review app for commit 5027a8c deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-5027a8c

=> QualifiedFunction -> m ()
trackFunctionP2Setup qf = do
fi <- withPathK "name" $ liftTx $ getFunctionInfo qf
void $ askTabInfo $ fiReturnType fi
Copy link
Member

Choose a reason for hiding this comment

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

Is this to check if the return table is tracked? If so, it should catch the error and return a more useful error message along these lines: "the return table: tname has to be tracked before the function can be tracked"

@hasura-bot
Copy link
Contributor

Review app for commit 4fae992 deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-4fae992

-> now fetch 'returns_table' bool value in 'raw_function_info' SQL
-> move 'raw_function_info' fetch SQL to separate file
   (src-rsr/function_info.sql)
-> throw "table <table-name> is not tracked" error message
0x777
0x777 previously approved these changes Jan 22, 2019
@hasura-bot
Copy link
Contributor

Review app for commit a22808e deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-a22808e

@hasura-bot
Copy link
Contributor

Review app for commit f2d8a54 deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-f2d8a54

@hasura-bot
Copy link
Contributor

Review app for commit f068d2f deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-f068d2f

@hasura-bot
Copy link
Contributor

Review app for commit 0b00abf deployed to Heroku: https://hge-ci-pull-1073.herokuapp.com
Docker image for server: hasura/graphql-engine:pull1073-0b00abf

@0x777 0x777 merged commit 0bf2457 into hasura:master Jan 25, 2019
@hasura-bot
Copy link
Contributor

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

@0x777 0x777 removed this from the v1.0.0-alpha36 milestone Jan 28, 2019
hasura-bot pushed a commit that referenced this pull request Sep 9, 2024
Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.127 to
1.0.128.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/serde-rs/json/releases">serde_json's
releases</a>.</em></p>
<blockquote>
<h2>1.0.128</h2>
<ul>
<li>Support serializing maps containing 128-bit integer keys to
serde_json::Value (<a
href="https://redirect.github.com/serde-rs/json/issues/1188">#1188</a>,
thanks <a
href="https://github.com/Mrreadiness"><code>@​Mrreadiness</code></a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/serde-rs/json/commit/d96b1d9b643b49a0bb92a8cf280daf6aa08f37cc"><code>d96b1d9</code></a>
Release 1.0.128</li>
<li><a
href="https://github.com/serde-rs/json/commit/599228d5dc52a0f1595fe4bd901821f70d0317a5"><code>599228d</code></a>
Merge pull request <a
href="https://redirect.github.com/serde-rs/json/issues/1188">#1188</a>
from Mrreadiness/feat/add-hashmap-key-128-serializer</li>
<li><a
href="https://github.com/serde-rs/json/commit/5416cee6c5ad355907f6e9d5f6039b994c69f768"><code>5416cee</code></a>
feat: add support for 128 bit HashMap key serialization</li>
<li><a
href="https://github.com/serde-rs/json/commit/27a4ca9d7a62394fe8f0103f3d91de59f055a4c4"><code>27a4ca9</code></a>
Upload CI Cargo.lock for reproducing failures</li>
<li>See full diff in <a
href="https://github.com/serde-rs/json/compare/1.0.127...1.0.128">compare
view</a></li>
</ul>
</details>
<br />

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=serde_json&package-manager=cargo&previous-version=1.0.127&new-version=1.0.128)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)

</details>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
V3_GIT_ORIGIN_REV_ID: c6df67f3a862c0eabd372688d42c4b68ca56a939
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.

expose postgres functions as queries and mutations