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

How do I generate API from my .graphql files without generating entire api? #39

Closed
jasonbobier opened this issue Aug 23, 2019 · 6 comments

Comments

@jasonbobier
Copy link

** Which Category is your question related to? **
amplify codegen for iOS

** What AWS Services are you utilizing? **
Appsync

** Provide additional details e.g. code snippets **
I don't want api for the entire schema polluting my namespace. I want API just for the .graphql file that I generate. How do I do this?

I was able to get this to work by moving the schema out of my ../graphql directory, putting my .graphql files in the directory, and then using:

amplify codegen --max-depth graphql/*.graphql

It generates the API.swift (which works!), but then errors out with:

uncaughtException: GraphQL schema file should contain a valid GraphQL introspection query result date=Fri Aug 23 2019 12:30:41 GMT+0200 (Central European Summer Time), pid=96247, uid=501, gid=20, cwd=/Users/jason/Documents/projects/tensil/bettercam-ios/Bettercam/Server/GraphQLAPI, execPath=/usr/local/bin/node, version=v10.16.3, argv=[/usr/local/bin/node, /usr/local/bin/amplify, codegen, --max-depth, graphql/GroupsTableViewControllerViewModel.graphql], rss=307851264, heapTotal=239886336, heapUsed=204516848, external=50348145, loadavg=[1.81005859375, 1.89892578125, 1.77294921875], uptime=676048, trace=[column=11, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-graphql-docs-generator/lib/generator/src/generator/generate.ts, function=Object.generate [as default], line=20, method=generate [as default], native=false, column=57, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-graphql-docs-generator/lib/src/index.ts, function=generate, line=40, method=null, native=false, column=11, file=/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-codegen/src/commands/statements.js, function=projects.forEach, line=32, method=forEach, native=false, column=7, file=internal/process/next_tick.js, function=process._tickCallback, line=68, method=_tickCallback, native=false], stack=[Error: GraphQL schema file should contain a valid GraphQL introspection query result, at Object.generate [as default] (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-graphql-docs-generator/lib/generator/src/generator/generate.ts:20:11), at generate (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-graphql-docs-generator/lib/src/index.ts:40:57), at projects.forEach (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-codegen/src/commands/statements.js:32:11), at process._tickCallback (internal/process/next_tick.js:68:7)]

@kaustavghosh06
Copy link
Contributor

@jasonbobier Could you please explain your use-case more in detail? What do you specifically mean by - I want API just for the .graphql file that I generate?
We recently released a way to run codegen without having to run amplify init or add an API via the CLI. Please take a look at Flow 5 here - https://aws-amplify.github.io/docs/cli-toolchain/graphql#workflows-

@jasonbobier
Copy link
Author

jasonbobier commented Oct 3, 2019

Right now, the codegen generates api for everything in the schema, including lots of stuff that I will never use. I want to be able to have .graphql files that I write that contain only my necessary queries.

For example, say you had a schema with something like this:

type Query {
	me: User
	users(ids: [ID!], emails: [String!]): [User!]
	mediaSession(id: ID!): MediaSession
	user(id: ID!): User
	group(id: ID!): Group
	groups(filter: ModelGroupFilterInput, limit: Int, nextToken: String): ModelGroupConnection
	mediaItem(id: ID!): MediaItem
	getLookup(key: String!): Lookup
	listLookups(
		key: String,
		filter: ModelLookupFilterInput,
		limit: Int,
		nextToken: String,
		sortDirection: ModelSortDirection
	): ModelLookupConnection
	editorProject(id: ID!): EditorProject
	editorScene(id: ID!): EditorScene
	editorMedia(id: ID!): EditorMedia
}

type User {
	id: ID!
	email: String!
	givenName: String!
	familyName: String!
	avatar: S3Object
	groups(sortDirection: SortDirection, limit: Int, nextToken: String): GroupConnection
	createdAt: AWSDateTime
	updatedAt: AWSDateTime
}

(assume the other types are large and complicated)

and I just want to get my email address with a query in a .graphql file like this:

query {
  me {
    email
  }
}

Running codegen will generate the entire schema API adding lots and lots of needless code and potential namespace issues instead of just the code for my email query.

I was able to get this to work by using the hack that I mentioned above, but it produces errors and is a totally hack. It would be better if the API produced was only what was in my .graphql files instead of the entire schema. Especially since many of these schemas are huge.

@SwaySway
Copy link
Contributor

SwaySway commented Oct 3, 2019

@jasonbobier
You can specify a custom Schema in includes in your .graphqlconfig.yml.
This file is located in the root of your directory.

See Below

projects:
  myProject:
    schemaPath: amplify/backend/api/myProject/build/schema.graphql
    includes:
      - src/customFolder/**/*.graphql # include your custom schema here
    excludes:
      - ./amplify/**
....

@jasonbobier
Copy link
Author

This isn't a custom schema though. These are just normal GraphQL client queries, mutations, and subscriptions. The while point of GraphQL is that the client can ask for only what it needs. Having the codegen download the real schema to use when validating client queries during codegen is what we want to happen, but we don't want the codegen to generate code for the entire schema. Just the queries that we specify in the .graphql files.

@renebrandel
Copy link
Contributor

@jasonbobier just wanted to let you know that we've publish an RFC to address this and other code generation issues. Would love to have your feedback! aws-amplify/amplify-cli#6898

@jasonbobier
Copy link
Author

Thanks! I'll take a look when I get a moment. The contract that I was working on with this ended a year ago though, so my memories are vague. :)

@phani-srikar phani-srikar removed enhancement type-gen Issues on graphql-type-generator Swift Target language: Swift labels Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants