-
Notifications
You must be signed in to change notification settings - Fork 57
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
Codegen Model Types #34
Comments
@ChrisChares GraphQL supports having only a subset of selection in each query and codegen generates this code so that the type has only the chosen selection set in the type of that operation. We will put this in the backlog to use Typescripts advanced Pick types |
@yuth thanks for the quick response. I figured the request-only-what-you-need nature of Graphql had something to do with the current implementation. Out of curiosity though, does that make sense in light of the fact that every one of a given model's fields are already included in all the auto generated queries and mutations (if sufficient depth)? Yes these full auto generated models will be incomplete (or worse misleading) when used with custom queries, but unless i'm missing something they'll map perfectly to what is currently autogenerated? |
The auto generated statements are meant to be used as starting point for an application and we expect them to either customize or add additional statements by adding new files to |
Sounds good, thanks for the explanation. |
Hi @ChrisChares - I currently do something like this:
And voila you have what you want. Of course in the case of partial query results you will sometimes have to cast the result as unknown then back to Room. I should look into Pick as @yuth suggested for sure. |
@hisham do you map the non-primitive (in Java, I guess) Scalar values to primitive Javascript? e.g. AWSTimestamp -> string, AWSJSON -> object? I've wondered how clean this would be irl. |
@jkeys-ecg-nmsu, I'm using typescript. Yes the codegen tool does map AWSTimestamp and AWSJSON to string in typescript. We did run into a couple of cases where we had to convert dates to specific string formats for it to be accepted by GraphQL as AWSTimestamp. Also AWSJSON (dynamodb specifically) for example does not like empty JSON fields so we deep clean the json before submitting them through the graphql endpoint. |
This feature would be very useful! |
This would really be great. Due to the selective queries, what about having a |
@ChrisChares @hisham
@hoegertn Have a look at this approach |
I messed with this for a while and finally gave up on Amplify doing this. Ironically Amplify uses this same code generator ( that I reference below ) to actually do its autogeneration. Add the following to your package:
Create a config.yml in the root:
NOTE: Then add the following to your package.json scripts:
|
I think the problem is basically that |
cc @gsans |
Edit: fixed my error by updating the graphql dependency of the project
|
Any plans from the team to implement this? |
I actually ended up removing the amplify codegen all together. Here is what I came up with: overwrite: true
schema:
- ${REACT_APP_SCHEMA_URL}:
method: GET
documents: "src/core/graphql/*.ts"
generates:
src/core/graphql/schema.graphql:
plugins:
- schema-ast:
includeDirectives: true
commentDescriptions: true
src/core/graphql/types.ts:
plugins:
- add:
content: '/* eslint-disable */'
- typescript:
skipTypename: true
src/core/graphql/operations.ts:
plugins:
- add:
content: '/* eslint-disable */'
- named-operations-object:
identifierName: Operations
src/core/graphql/:
preset: near-operation-file
presetConfig:
baseTypesPath: types.ts
extension: .ts
plugins:
- add:
content: '/* eslint-disable */'
- typescript-operations:
namingConvention: pascal-case#pascalCase
skipTypename: true
# preResolveTypes: true
- typescript-document-nodes:
namingConvention: camel-case#camelCase and then I made a REST API endpoint that would let me get the graphql schema that looks like this: import { AppSync } from 'aws-sdk';
import { Request, Response } from 'express';
const client = new AppSync();
const apiId = process.env.API_API_GRAPHQLAPIIDOUTPUT as string;
const apiKey = process.env.API_API_GRAPHQLAPIKEYOUTPUT as string;
export async function schemaHandler(req: Request, res: Response) {
try {
const req = client.getIntrospectionSchema({
apiId,
format: 'JSON',
includeDirectives: true
});
req.on('build', () => {
req.httpRequest.headers['x-api-key'] = apiKey;
});
const result = await req.promise();
return res
.type('application/json')
.send((result.schema as any).toString());
} catch (error) {
return res
.status(500)
.json({ success: false, errors: ['Failed to get schema'] });
}
} |
Amplify codegen is not using the guild's graphql-code-generator typescript plugin but is based on the Apollo generator. Hence the flag is not applicable to the package. However we do notice that there are some advantages in guild's graphql generator system and consider the tradeoffs of migrating to it. As for the model type generation mentioned above , we just merged one PR pended on the release. Last but not least, since most of amplify codegen users are mostly using amplify framework and appsync service, we would like to develop the codegen as a more amplify/appsync/datastore oriented generator, which is different from others. A redesign for amplify codegen is under the plan and we are welcome for any thoughts or feedbacks so that we can provide a better developer experience. |
This is similar to #32. Closing this in favor of that. |
Totally a necrobump but for those in the future I utilized the following type to stripe /**
* Exclude __typename from the type for API responses.
*
* combining {@link https://grrr.tech/posts/2021/typescript-partial/#in-conclusion Making TypeScript's Partial type work for nested objects} and {@link https://github.com/aws-amplify/amplify-codegen/issues/34#issuecomment-610964922}
*
*/
export type Response<K> = {
[attr in keyof K]?: K[attr] extends object
? Response<K[attr]>
: K[attr] extends object | null
? Response<K[attr]> | null
: K[attr] extends object | null | undefined
? Response<K[attr]> | null | undefined
: Omit<Exclude< K[attr], null>, "__typename">;
}; |
Is your feature request related to a problem? Please describe.
It's frustrating that codegen doesn't generate typescript model types. If I define a @model in my schema consisting of:
Codegen will generate literally thousands of lines of code. For example:
But it never generates a simple
Room
model interface. We have to create and maintain this manually. Why? Is there a reason for this?Describe the solution you'd like
For this specific example, somewhere in the thousands of lines of code in
API.ts
I would expect to find:Describe alternatives you've considered
Manual creation and maintenance of model types.
Thanks for your time,
Chris
The text was updated successfully, but these errors were encountered: