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

feat(GraphQL): Custom logic now supports DQL queries #6115

Merged
merged 4 commits into from
Jul 30, 2020

Conversation

abhimanyusinghgaur
Copy link
Contributor

@abhimanyusinghgaur abhimanyusinghgaur commented Jul 29, 2020

Fixes GraphQL-600.

This PR adds DQL query (previously known as GraphQL+-) support in GraphQL through the @custom directive. So, now it is possible to do something like this in the GraphQL schema:

type Tweets {
	id: ID!
	text: String! @search(by: [fulltext])
	user: User
	timestamp: DateTime! @search
}
type User {
	screen_name: String! @id
	followers: Int @search
	tweets: [Tweets] @hasInverse(field: user)
}
type UserTweetCount @remote {
	screen_name: String
	tweetCount: Int
}

type Query {
  dqlTweetsByAuthorFollowers: [Tweets] @custom(dql: """
	query {
		var(func: type(Tweets)) @filter(anyoftext(Tweets.text, "DQL")) {
			Tweets.user {
				followers as User.followers
			}
			userFollowerCount as sum(val(followers))
		}
		dqlTweetsByAuthorFollowers(func: uid(userFollowerCount), orderdesc: val(userFollowerCount)) {
			id: uid
			text: Tweets.text
			timestamp: Tweets.timestamp
		}
	}
	""")
	
  filteredTweetsByAuthorFollowers(search: String!): [Tweets] @custom(dql: """
	query t($search: string) {
		var(func: type(Tweets)) @filter(anyoftext(Tweets.text, $search)) {
			Tweets.user {
				followers as User.followers
			}
			userFollowerCount as sum(val(followers))
		}
		filteredTweetsByAuthorFollowers(func: uid(userFollowerCount), orderdesc: val(userFollowerCount)) {
			id: uid
			text: Tweets.text
			timestamp: Tweets.timestamp
		}
	}
	""")

  queryUserTweetCounts: [UserTweetCount] @custom(dql: """
	query {
		queryUserTweetCounts(func: type(User)) {
			screen_name: User.screen_name
			tweetCount: count(User.tweets)
		}
	}
	""")
}

And then you can send a GraphQL query to execute the DQL, like this:

query {
  dqlTweetsByAuthorFollowers {
    id
    text
    timestamp
  }
  filteredTweetsByAuthorFollowers(search: "hello") {
    text
  }
  queryUserTweetCounts {
    screen_name
    tweetCount
  }
}

This change is Reviewable

@github-actions github-actions bot added the area/graphql Issues related to GraphQL support on Dgraph. label Jul 29, 2020
@abhimanyusinghgaur abhimanyusinghgaur changed the title feat(GraphQL): add dql in custom logic feat(GraphQL): add DQL in custom logic Jul 30, 2020
@abhimanyusinghgaur abhimanyusinghgaur changed the title feat(GraphQL): add DQL in custom logic feat(GraphQL): Custom logic now supports DQL queries Jul 30, 2020
Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 43 of 43 files at r1.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @abhimanyusinghgaur)


graphql/resolve/query.go, line 122 at r1 (raw file):

		args := query.Arguments()
		for k, v := range args {
			vStr, err := x.ScalarToString(v)

Add a comment about why do we convert all scalars to string.


graphql/resolve/query.go, line 127 at r1 (raw file):

					k))
			}
			vars["$"+k] = vStr

Also add a comment about this that Query in Alpha expects variable names to be prefixed with $.


graphql/schema/rules.go, line 1226 at r1 (raw file):

				typ.Name, field.Name))
		}
		if _, err := gql.Parse(gql.Request{Str: dqlArg.Value.Raw}); err != nil {

We don't need to these for now.


graphql/schema/wrappers.go, line 1122 at r1 (raw file):

func (q *query) DQLQuery() string {
	if customDir := q.op.inSchema.customDirectives["Query"][q.Name()]; customDir != nil {
		if dqlArg := customDir.Arguments.ForName(dqlArg); dqlArg != nil {

if arg :=


x/x.go, line 1048 at r1 (raw file):

func ScalarToString(val interface{}) (string, error) {
	var str string

Do we need int8, int16 etc. here or only bool, string and float64? Can you verify once and maybe just keep this function in graphql package itself

Copy link
Contributor Author

@abhimanyusinghgaur abhimanyusinghgaur left a comment

Choose a reason for hiding this comment

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

Reviewable status: 36 of 42 files reviewed, 5 unresolved discussions (waiting on @pawanrawal)


graphql/resolve/query.go, line 122 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Add a comment about why do we convert all scalars to string.

Done.


graphql/resolve/query.go, line 127 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Also add a comment about this that Query in Alpha expects variable names to be prefixed with $.

Done.


graphql/schema/rules.go, line 1226 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

We don't need to these for now.

Done.


graphql/schema/wrappers.go, line 1122 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

if arg :=

Done.


x/x.go, line 1048 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Do we need int8, int16 etc. here or only bool, string and float64? Can you verify once and maybe just keep this function in graphql package itself

Done.

@abhimanyusinghgaur abhimanyusinghgaur marked this pull request as ready for review July 30, 2020 12:29
@abhimanyusinghgaur abhimanyusinghgaur merged commit 117ac3c into master Jul 30, 2020
@abhimanyusinghgaur abhimanyusinghgaur deleted the abhimanyu/custom-dql branch July 30, 2020 17:16
pawanrawal pushed a commit that referenced this pull request Jul 31, 2020
Fixes GraphQL-600.

This PR adds DQL query (previously known as GraphQL+-) support in GraphQL through the `@custom` directive.

(cherry picked from commit 117ac3c)
gja pushed a commit that referenced this pull request Aug 19, 2020
Fixes GraphQL-600.

This PR adds DQL query (previously known as GraphQL+-) support in GraphQL through the `@custom` directive.

(cherry picked from commit 117ac3c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/graphql Issues related to GraphQL support on Dgraph.
Development

Successfully merging this pull request may close these issues.

2 participants