2.3.4 • Published 4 years ago

graphoose v2.3.4

Weekly downloads
73
License
-
Repository
github
Last release
4 years ago

graphoose

Transform a graphql type into a mongoose model

import * as graphoose from 'graphoose'

const user = gql`
type User {
  _id: ID !
  email: String !
  dob: Date !
}
`
const User = graphoose.model(user)

await User.findOne({ email: 'joe@doe.com' })

Note: graphoose accepts either a string or a graphql object

Return fields only

graphoose.fields('type F { a: Int }') // { a: { type: Number } }

Return schema only

const F = graphoose.schema('type F { a: Int }')
mongoose.model('Foo', schema)

Scalars

GraphqlMongoose
BooleanBoolean
BufferBuffer
DateDate
FloatDecimal128
IDObjectId
IntNumber
StringString
StringString

Unsupported mongoose types

Unsupported GraphQL types

There is no (yet) support for the following types:

  • Union
  • Enum

You can emulate enum via the enum directive (see below)

Nested schema

const TEAM = gql`
type Team {
  name: String !
}
`
const PLAYER = gql`
type Player {
  _id: ID !
  name: String !
  team: Team !
}
`
const team = graphoose.fields(TEAM)
graphoose.model(PLAYER, { nested: { team } })

Directives

You can use the following directives. You can find their declarations here

You can rename a directive by passing its new name, let's say you rename the directive ref to link:

const Player = gql`
type Player {
  _id: ID !
  team: ID ! @link(model: "Team")
}
`
graphoose.model(Player, { directives: { ref: 'link' } })

You can also invalidate a directive like this { directives: { ref: false } }

References

type Player {
  _id: ID !
  team: ID ! @ref(model: "Team")
}

Indexes

type Player {
  _id: ID !
  name: String ! @unique
  age: Int ! @index
  email: String ! @sparse
}

Default value

type Player {
  _id: ID !
  score: Int ! @default(value: 100)
}

Alias

type Player {
  _id: ID !
  score: Int ! @alias(name: "points")
}

String

type Player {
  _id:        ID !
  username:   String !  @lowercase @minlength(length: 2) @trim
  lastname:   String    @uppercase @trim @maxlength(length: 100)
  email:      String !  @match(expression: "^.+@.+\..+$" modifiers: "i")
  power:      String    @enum(values: ["up" "down"])
}

Number an dates

type Player {
  _id:        ID !
  score:      Int !     @min(value: 2) @max(value: 16)
  epoch:      Int !     @enum(values: [2 4 16]) 
  date:       Date !    @min(date: '2010-01-01' max: '2020-01-01')
}
2.3.2

4 years ago

2.3.1

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.2.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago