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

MongoDB _id field #676

Open
drksbr opened this issue Aug 6, 2023 · 0 comments
Open

MongoDB _id field #676

drksbr opened this issue Aug 6, 2023 · 0 comments

Comments

@drksbr
Copy link

drksbr commented Aug 6, 2023

Hey guys.

It must be silly, but I'm not managing to evolve in this issue of ID fields.

Example:

# schema.graphql

type Mutation {
   createISP(isp: NewISP!): ISP!
}

type ISP {
   ID: ID!
   Name: String!
   Location: String!
   Devices: [Devices]
}

input NewISP {
   Name: String!
   Location: String!
}

// isp.resolver.go

type ISP struct {
IDField graphql.ID
NameField string
LocationField string
DevicesField *[]*Device
}

func (i *ISP) ID() graphql.ID {
	return i.IDField
}

func (i *ISP) Name() string {
	return i.NameField
}

func (i *ISP) Location() string {
	return i.LocationField
}

func (r *Resolver) GetISPs() []*ISP {

	var results []*ISP
	cursor, err := DB.Find(context.Background(), bson.M{})
	if err != nil {
		return nil
	}
	if err := cursor.All(context.Background(), &results); err != nil {
		return nil
	}
	return results
}

type NewISP struct {
	Name     string
	Location string
}

func (r *Resolver) CreateISP(args *struct{ Isp NewISP }) *ISP {

	// create new
	ispId := randomID() // Here I make a fake ID for testing because I couldn't use the id of the mongo object.

	isp := &ISP{
		IDField:       graphql.ID(ispId),
		NameField:     args.Isp.Name,
		LocationField: args.Isp.Location,
	}

	DB.InsertOne(ctx, isp)

	return isp
}

  • I'm wanting to use _id as an identification field and not need to create a new field for it as I did with IdField
    image

Can you help me with this issue?

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

1 participant