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(utils): @pgQuery support for scalars #534

Merged
merged 4 commits into from Oct 15, 2019

Conversation

phryneas
Copy link
Contributor

@phryneas phryneas commented Oct 8, 2019

We had already chatted about this - this PR allows for scalars to be added as subqueries using the @pgQuery directive with the fragment argument.

If this is merged, it would be great if you could mention my employer Mayflower in the release notes, as this PR was done on company time. But if this something you don't do, thats fine, too 😃

@phryneas
Copy link
Contributor Author

phryneas commented Oct 8, 2019

As for that failing test... I have no idea what that means. Doesn't seem to fail in every environment?

@phryneas
Copy link
Contributor Author

phryneas commented Oct 8, 2019

Okay, figured it out - my test-SQL needed a CAST for Postgres 9. Tests are now working fine :)

@benjie
Copy link
Member

benjie commented Oct 10, 2019

Awesome; from a superficial glance this looks great but I'll try and review it properly next week. In the mean time, please change your CAST(${...} AS type) to ${...}::type for consistency with the rest of the project 😁

@phryneas
Copy link
Contributor Author

sure thing 👍
should be consistent now.

],
});
const printedSchema = printSchema(schema);
expect(printedSchema).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

Once #533 lands, can you rebase this pull request on top of it and simplify this test? You'll no longer need to call printSchema() yourself, you can just do expect(schema).toMatchSnapshot(). 😄

I'm not sure when that PR will land, but I wanted to give you a heads-up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do, please ping me when it lands

Copy link
Member

@benjie benjie left a comment

Choose a reason for hiding this comment

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

Nice; this seems to work well! Couple minor bits of feedback to address, plus I just merged #533 so please rebase and regenerate your snapshots 👍

fragment: ${embed(sql.fragment`(SELECT 100)`)}
)
myCustomScalarWithFunction: Int! @pgQuery(
fragment: ${embed(queryBuilder => sql.fragment`(SELECT 101)`)}
Copy link
Member

Choose a reason for hiding this comment

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

We need to test references to the graphile_utils.users table itself, so please can you change this to return a String and do something like:

Suggested change
fragment: ${embed(queryBuilder => sql.fragment`(SELECT 101)`)}
fragment: ${embed(queryBuilder => sql.fragment`(${queryBuilder.getTableAlias()}.name || ' ' || ${queryBuilder.getTableAlias()}.email)`)}

name
myCustomScalar
myCustomScalarWithFunction
myCustomScalarWithFunctionAndArgument(test: 102)
Copy link
Member

Choose a reason for hiding this comment

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

Check that these work as expected with aliases too, please.

Suggested change
myCustomScalarWithFunctionAndArgument(test: 102)
myCustomScalarWithFunctionAndArgument(test: 102)
m103: myCustomScalarWithFunctionAndArgument(test: 103)
m104: myCustomScalarWithFunctionAndArgument(test: 104)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing you want one alias for each one of them, not the same one twice?

Copy link
Member

Choose a reason for hiding this comment

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

These are different aliases? You might have missed the red line was a deletion... not sure why GitHub did that 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, but they were both aliases for myCustomScalarWithFunctionAndArgument, so I wasn't sure if that was really your intention or if you wanted one alias for myCustomScalar, one for myCustomScalarWithFunctionAndArgument, etc.

ah well, seems like what I did worked out for you :D

extend type User {
myCustomArrayOfScalars: [String!]! @pgQuery(
fragment: ${embed(
sql.fragment`array(SELECT name from ${sql.fragment`graphile_utils.pets`})`
Copy link
Member

Choose a reason for hiding this comment

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

This is equivalent to:

Suggested change
sql.fragment`array(SELECT name from ${sql.fragment`graphile_utils.pets`})`
sql.fragment`array(SELECT name from graphile_utils.pets)`

I've never used the array constructor in this way; I've always done select array_agg(name) from ... instead. It seems that this might actually be faster because it's simpler so I should definitely experiment with its usage in PostGraphile.

Copy link
Member

Choose a reason for hiding this comment

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

Filed: #543

@phryneas
Copy link
Contributor Author

@benjie I think I adressed everything

@phryneas phryneas requested a review from benjie October 15, 2019 14:49
Copy link
Member

@benjie benjie left a comment

Choose a reason for hiding this comment

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

Awesome! If you have a few more minutes it would be great if you could incorporate this into the makeExtendSchemaPlugin docs: https://github.com/graphile/graphile.github.io/edit/develop/src/pages/postgraphile/make-extend-schema-plugin.md

@benjie benjie changed the title @pgQuery.fragment scalar functionality feat(utils): @pgQuery support for scalars Oct 15, 2019
@benjie benjie merged commit 49259c2 into graphile:master Oct 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants