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

Remove field from InputType #34

Open
israelglar opened this issue Feb 6, 2019 · 9 comments
Open

Remove field from InputType #34

israelglar opened this issue Feb 6, 2019 · 9 comments

Comments

@israelglar
Copy link
Contributor

I have this 2 types that have a relation between them.

export const Author = {
  table: 'author',
  fields: {
    name: StringType,
    birthday: DateType,
  },
};

export const Book = {
  table: 'books',
  fields: {
    _id: MongoIdType,
    title: StringType,
    pages: IntType,
    authorIds: MongoIdArrayType,
  },
  relationships: {
    authors: {
      get type() {
        return Author;
      },
      fkField: 'authorIds',
    },
  },
}
type Book {
  _id: String
  title: String
  pages: Int
  authorIds: [String]
  authors(...): [Author]
}

type BookInput {
  _id: String
  title: String
  pages: Int
  authorIds: [String]
  authors: [AuthorInput]
}

What I want is to give the user the possibility to when getting a book, to also get the author of that book. But when creating a book, only send the authorsIds array of that book.

Is there an option so that I can remove a field from the Input type? And only from the Input Type?

The end result should be:

type Book {
  _id: String
  title: String
  pages: Int
  authorIds: [String]
  authors(...): [Author]
}

type BookInput {
  _id: String
  title: String
  pages: Int
  authorIds: [String]
}
@arackaf
Copy link
Owner

arackaf commented Feb 6, 2019

Am I understanding correctly:

You want to only be able to provide authorIs when creating a new book (which you can already do). But you want the GraphQL schema to prevent a user from sending a new author object when creating a book, which would result in that author being created then and there.

Is that right?

If so, no, as of now there's no way to remove the authors from the input type, but it's something I'd be willing to consider after version 0.8 comes out.

@israelglar
Copy link
Contributor Author

Yes, it's exactly that. I already have an idea that works for a PR. I will submit so you can see it

@arackaf
Copy link
Owner

arackaf commented Feb 6, 2019

Cool

@arackaf
Copy link
Owner

arackaf commented Feb 6, 2019

Hey do me a favor, if you're looking to PR, please send it off the version-8 branch

@israelglar
Copy link
Contributor Author

I'm considering using your library in one of my projects, and I have a couple more questions:

  1. Is there a way to set default values to the fields of generated resolvers?

  2. Is there a way to have the fields accept GraphQLType? Something like:

const newType = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    hello: {
      type: GraphQLString,
      resolve() {
        return 'world';
      },
    },
  },
});
export const Book = {
  table: 'books',
  fields: {
    createdType: newType,
  },
}

What I really want with this is to be able to define an enum for graphql.

I'm still getting to know the library code and I don't know what to do to accomplish these points. But with some guidance I can create a PR to have these 2 functionalities as well :)

Thanks!

@arackaf
Copy link
Owner

arackaf commented Feb 6, 2019

Yeah the code is pretty horrible at the moment. Some major cleanup + TypeScript is on my ToDo list.

Not sure how you'd do default values, or what that'd even mean, here. So right now whatever fields you request will be queried from Mongo. Your best bet might be the adjustResults hook

https://github.com/arackaf/mongo-graphql-starter#all-available-hooks

I'm not sure what the GraphQL type does, but I do have the JSON type implemented, so common types can absolutely be baked in, here. Check out

createMasterSchema.js, createMasterResolver.js and createTypeSchema in src/codeGen

@israelglar
Copy link
Contributor Author

For the default values I was thinking something like:

export const Book = {
  table: 'books',
  fields: {
    fieldWithDefault: {
      type: StringType,
      defaultValue: "a good book"
    },
  },
};

And what would do is set the default value for the InputType. But maybe that's not the best approach.. Maybe it would be better to set default values for arguments in resolver and not in the Type itself. But that would be more complex..

The result would be:

type BookInput {
  _id: String
  fieldWithDefault: String = "a good book"
}

About the GraphQL type, it's just a easy way in graphql library to create and manage graphql Types. You can see it in use here https://github.com/graphql/graphql-js#using-graphqljs

@arackaf
Copy link
Owner

arackaf commented Feb 6, 2019

Default values in the input type is actually the only option that makes sense to me. I could then apply those defaults in the resolver for the create mutation.

What would default values in the resolver do, specifically? Afraid I don't quite follow what you might be going for, there?

@israelglar
Copy link
Contributor Author

I was thinking that adding to the input type would add to all the Resolvers, so you would have the update with the default value set..

But only adding to the create resolver, like you said, makes sense and solves that :)

@arackaf arackaf reopened this Feb 9, 2023
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

No branches or pull requests

2 participants