aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.82k stars 820 forks source link

What is the simple way to add custom queries #2420

Closed raphdom closed 5 years ago

raphdom commented 5 years ago

Which Category is your question related to? API

What AWS Services are you utilizing? AWS App Sync

Provide additional details e.g. code snippets My schema is the following:

type Song @model 
@auth(rules: [{allow: owner}])
{
  id: ID!
  name: String!
  lyric: String!
  author: String
  owner: String
}

I'm using Angular and this generate the query to list all songs, but I want to restrict the result to have only the id, name and the author, what is the simple way to do this? I cannot found this in the documentation and many issues here talk about create a custom query but how can I do this?

kaustavghosh06 commented 5 years ago

@raphdom Do you want to create a Query in your schema or a custom query in the client-side?

raphdom commented 5 years ago

In the client side

kaustavghosh06 commented 5 years ago

If you just want to not receive fields on the client-side without any server side protection, you could just use a query like this:

query ListSongs{
  listSongs {
    items {
      id
      name
      author
  }
}
raphdom commented 5 years ago

Yeah, okay, but in which file I should put this, and how can I regenerate the API.service.ts with this change, I mean with another method to use this query.

kaustavghosh06 commented 5 years ago

You can add this query in src/graphl directory of your project in a separate file - say custom-queries.ts and then run amplify codegen again to regenerate the API.service.ts having the queries present in your custom-queries.ts file.

SwaySway commented 5 years ago

If you have any other questions please feel free to comment on this issue.

harryvu commented 4 years ago

I am reading the Full Stack Serverless book from oreilly and tried to create custom queries and follow the same suggestion here without successful. I have a very simple model below:

type Note @model { id: ID! clientId: ID name: String! description: String completed: Boolean }

and the original getNote query:

export const getNote = / GraphQL / query GetNote($id: ID!) { getNote(id: $id) { id clientId name description completed } } ;

I have created new file named simple-queries.js with the following content:

export const getSimpleNote = / GraphQL / query GetSimpleNote($id: ID!) { getSimpleNote(id: $id) { id clientId name } } ;

then ran the following command:

$ amplify api gql-compile GraphQL schema compiled successfully.

what I was expected is the new Query.getSimpleNote.req.vtl (and Query.getSimpleNote.res.vtl) should be generated under the build/resolvers folder, but they are nowhere to be found. What am I missing? BTW, I am using React and Node JS.

lukeramsden commented 4 years ago

@SwaySway How would you create a query on the schema side? Putting a query in my schema.graphql produces this error:

Error: Found a OperationDefinition. Transformers accept only documents consisting of TypeSystemDefinitions.
joostfarla commented 4 years ago

@harryvu: Adding a custom query with a compact representation of an existing type does not involve any changes to the API (server-side). Hence, there are no resolver changes needed to support this query. The only thing that changes is the query on the client-side.

lkolodziejek commented 4 years ago

It took me 2 days to solve this. Still, I wasn't able to find answer to this question in documentation, any blog or forum. This really should be properly documented, that's basic functionality.

You need to put query definition in the file in /src/graphql and call file *.graphql (e.g. customer-queries.graphql). File needs to contain definition of query only (without "export const...)"

lukeramsden commented 4 years ago

I agree that custom query codegen is poorly documented, and for people that are newcomers to GraphQL and its accompanying codegen tooling getting started with Amplify it's not particularly intuitive. I'd make a PR but I've moved away from the Amplify API in favour of Postgraphile, and I don't remember what I was doing for document codegen!

sumeetchawla commented 4 years ago

You can add this query in src/graphl directory of your project in a separate file - say custom-queries.ts and then run amplify codegen again to regenerate the API.service.ts having the queries present in your custom-queries.ts file.

You guys are doing a great job but to be honest the documentation is missing a lot of stuff:(

Example: Why isn't this custom-queries mentioned anywhere in any documentation? I searched high and low about this and only found out about it in this thread.

Similarly, there are many other features which are completely missing from documentation and any new adopter of Amplify will get really frustrated with these issues :(

Nxtra commented 4 years ago

It took me 2 days to solve this. Still, I wasn't able to find answer to this question in documentation, any blog or forum. This really should be properly documented, that's basic functionality.

You need to put query definition in the file in /src/graphql and call file *.graphql (e.g. customer-queries.graphql). File needs to contain definition of query only (without "export const...)"

This is to generate a custom query on the backend I suppose?

Whilst @sumeetchawla answer is for the frontend?

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.