Docs
खोज⌘ K
  • Home
  • The Graph के बारे में
  • समर्थित नेटवर्क
  • Protocol Contracts
  • सबग्राफ
    • सबस्ट्रीम
      • टोकन API
        • AI Suite
          • Overview
          • Indexer टूलिंग
            • GraphTally Guide
            • Supported Network Requirements
            • Chain Integration Process Overview
            • नई श्रृंखला एकीकरण
            • संसाधन
              सबग्राफ > विकसित करना > बनाने

              10 मिनट

              The Graph QL Schema

              Overview

              The schema for your Subgraph is in the file schema.graphql. GraphQL schemas are defined using the GraphQL interface definition language.

              Note: If you’ve never written a GraphQL schema, it is recommended that you check out this primer on the GraphQL type system. Reference documentation for GraphQL schemas can be found in the GraphQL API section.

              संपत्ति परिभाषित करना

              इससे पहले कि आप एन्टिटीज को परिभाषित करें, यह महत्वपूर्ण है कि आप एक कदम पीछे हटें और सोचें कि आपका डेटा कैसे संरचित और लिंक किया गया है।

              • All queries will be made against the data model defined in the Subgraph schema. As a result, the design of the Subgraph schema should be informed by the queries that your application will need to perform.
              • यह उपयोगी हो सकता है कि संस्थाओं की कल्पना ‘डेटा’ समाहित करने वाले वस्तुओं के रूप में की जाए, न कि घटनाओं या कार्यों के रूप में।
              • You define entity types in schema.graphql, and Graph Node will generate top-level fields for querying single instances and collections of that entity type.
              • Each type that should be an entity is required to be annotated with an @entity directive.
              • डिफ़ॉल्ट रूप से, संस्थाएँ परिवर्तनीय होती हैं, जिसका अर्थ है कि मैपिंग मौजूदा संस्थाओं को लोड कर सकती हैं, उन्हें संशोधित कर सकती हैं और उस संस्थान के एक नए संस्करण को संग्रहीत कर सकती हैं।
                • Mutability comes at a price, so for entity types that will never be modified, such as those containing data extracted verbatim from the chain, it is recommended to mark them as immutable with @entity(immutable: true).
                • अगर किसी एंटिटी को बनाने वाले उसी ब्लॉक में परिवर्तन होते हैं, तो मैपिंग्स अम्यूटेबल एंटिटीज़ में परिवर्तन कर सकती हैं। अम्यूटेबल एंटिटीज़ को लिखने और क्वेरी करने में बहुत तेज़ होती हैं, इसलिए इन्हें संभव होने पर उपयोग करना चाहिए।

              अच्छा उदाहरण

              The following Gravatar entity is structured around a Gravatar object and is a good example of how an entity could be defined.

              1type Gravatar @entity(immutable: true) {2  id: Bytes!3  owner: Bytes4  displayName: String5  imageUrl: String6  accepted: Boolean7}

              खराब उदाहरण

              The following example GravatarAccepted and GravatarDeclined entities are based around events. It is not recommended to map events or function calls to entities 1:1.

              1type GravatarAccepted @entity {2  id: Bytes!3  owner: Bytes4  displayName: String5  imageUrl: String6}78type GravatarDeclined @entity {9  id: Bytes!10  owner: Bytes11  displayName: String12  imageUrl: String13}

              वैकल्पिक और आवश्यक फ़ील्ड

              Entity fields can be defined as required or optional. Required fields are indicated by the ! in the schema. If the field is a scalar field, you get an error when you try to store the entity. If the field references another entity then you get this error:

              1गैर-शून्य फ़ील्ड 'नाम' के लिए हल किया गया शून्य मान

              Each entity must have an id field, which must be of type Bytes! or String!. It is generally recommended to use Bytes!, unless the id contains human-readable text, since entities with Bytes! id’s will be faster to write and query as those with a String! id. The id field serves as the primary key, and needs to be unique among all entities of the same type. For historical reasons, the type ID! is also accepted and is a synonym for String!.

              For some entity types the id for Bytes! is constructed from the id’s of two other entities; that is possible using concat, e.g., let id = left.id.concat(right.id) to form the id from the id’s of left and right. Similarly, to construct an id from the id of an existing entity and a counter count, let id = left.id.concatI32(count) can be used. The concatenation is guaranteed to produce unique id’s as long as the length of left is the same for all such entities, for example, because left.id is an Address.

              बिल्ट-इन स्केलर प्रकार

              ग्राफक्यूएल समर्थित स्केलर्स

              नीचे दिए गए स्केलर्स GraphQL API में समर्थित हैं:

              प्रकारDescription
              Bytesबाइट सरणी, एक हेक्साडेसिमल स्ट्रिंग के रूप में दर्शाया गया है। आमतौर पर एथेरियम हैश और पतों के लिए उपयोग किया जाता है।
              StringScalar for string values. Null characters are not supported and are automatically removed.
              BooleanScalar for boolean values.
              IntThe GraphQL spec defines Int to be a signed 32-bit integer.
              Int8An 8-byte signed integer, also known as a 64-bit signed integer, can store values in the range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Prefer using this to represent i64 from ethereum.
              BigIntLarge integers. Used for Ethereum’s uint32, int64, uint64, …, uint256 types. Note: Everything below uint32, such as int32, uint24 or int8 is represented as i32.
              BigDecimalBigDecimal High precision decimals represented as a significand and an exponent. The exponent range is from −6143 to +6144. Rounded to 34 significant digits.
              TimestampIt is an i64 value in microseconds. Commonly used for timestamp fields for timeseries and aggregations.

              Enums

              आप स्कीमा के भीतर एनम भी बना सकते हैं। Enums में निम्न सिंटैक्स होता है:

              1enum TokenStatus {2  OriginalOwner3  SecondOwner4  ThirdOwner5}

              Once the enum is defined in the schema, you can use the string representation of the enum value to set an enum field on an entity. For example, you can set the tokenStatus to SecondOwner by first defining your entity and subsequently setting the field with entity.tokenStatus = "SecondOwner". The example below demonstrates what the Token entity would look like with an enum field:

              More detail on writing enums can be found in the GraphQL documentation⁠.

              निकाय संबंध

              एक इकाई का आपकी स्कीमा में एक या अधिक अन्य संस्थाओं से संबंध हो सकता है। आपके प्रश्नों में इन संबंधों का पता लगाया जा सकता है। ग्राफ़ में रिश्ते यूनिडायरेक्शनल हैं। रिश्ते के किसी भी “अंत” पर एक यूनिडायरेक्शनल रिश्ते को परिभाषित करके द्विपक्षीय संबंधों को अनुकरण करना संभव है।

              संस्थाओं पर संबंधों को किसी अन्य क्षेत्र की तरह ही परिभाषित किया जाता है सिवाय इसके कि निर्दिष्ट प्रकार किसी अन्य इकाई का है।

              एक-से-एक संबंध

              Define a Transaction entity type with an optional one-to-one relationship with a TransactionReceipt entity type:

              1type Transaction @entity(immutable: true) {2  id: Bytes!3  transactionReceipt: TransactionReceipt4}56type TransactionReceipt @entity(immutable: true) {7  id: Bytes!8  transaction: Transaction9}

              एक से कई रिश्ते

              Define a TokenBalance entity type with a required one-to-many relationship with a Token entity type:

              1type Token @entity(immutable: true) {2  id: Bytes!3}45type TokenBalance @entity {6  id: Bytes!7  amount: Int!8  token: Token!9}

              रिवर्स लुकअप

              Reverse lookups can be defined on an entity through the @derivedFrom field. This creates a virtual field on the entity that may be queried but cannot be set manually through the mappings API. Rather, it is derived from the relationship defined on the other entity. For such relationships, it rarely makes sense to store both sides of the relationship, and both indexing and query performance will be better when only one side is stored and the other is derived.

              For one-to-many relationships, the relationship should always be stored on the ‘one’ side, and the ‘many’ side should always be derived. Storing the relationship this way, rather than storing an array of entities on the ‘many’ side, will result in dramatically better performance for both indexing and querying the Subgraph. In general, storing arrays of entities should be avoided as much as is practical.

              उदाहरण

              We can make the balances for a token accessible from the token by deriving a tokenBalances field:

              1type Token @entity(immutable: true) {2  id: Bytes!3  tokenBalances: [TokenBalance!]! @derivedFrom(field: "token")4}56type TokenBalance @entity {7  id: Bytes!8  amount: Int!9  token: Token!10}

              Here is an example of how to write a mapping for a Subgraph with reverse lookups:

              1let token = new Token(event.address) // Create Token2token.save() // tokenBalances is derived automatically34let tokenBalance = new TokenBalance(event.address)5tokenBalance.amount = BigInt.fromI32(0)6tokenBalance.token = token.id // Reference stored here7tokenBalance.save()

              अनेक-से-अनेक संबंध

              मैनी-टू-मैनी संबंधों के लिए, जैसे कि प्रत्येक उपयोगकर्ता किसी भी संख्या में संगठनों से संबंधित हो सकता है, सबसे सरल, लेकिन आम तौर पर सबसे अधिक प्रदर्शनकारी नहीं, संबंध को मॉडल करने का तरीका शामिल दो संस्थाओं में से प्रत्येक में एक सरणी के रूप में है। यदि संबंध सममित है, तो संबंध के केवल एक पक्ष को संग्रहित करने की आवश्यकता है और दूसरे पक्ष को व्युत्पन्न किया जा सकता है।

              उदाहरण

              Define a reverse lookup from a User entity type to an Organization entity type. In the example below, this is achieved by looking up the members attribute from within the Organization entity. In queries, the organizations field on User will be resolved by finding all Organization entities that include the user’s ID.

              1type Organization @entity {2  id: Bytes!3  name: String!4  members: [User!]!5}67type User @entity {8  id: Bytes!9  name: String!10  organizations: [Organization!]! @derivedFrom(field: "members")11}

              A more performant way to store this relationship is through a mapping table that has one entry for each User / Organization pair with a schema like

              1type Organization @entity {2  id: Bytes!3  name: String!4  members: [UserOrganization!]! @derivedFrom(field: "organization")5}67type User @entity {8  id: Bytes!9  name: String!10  organizations: [UserOrganization!] @derivedFrom(field: "user")11}1213type UserOrganization @entity {14  id: Bytes! # Set to `user.id.concat(organization.id)`15  user: User!16  organization: Organization!17}

              इस दृष्टिकोण के लिए आवश्यक है कि प्रश्नों को पुनः प्राप्त करने के लिए एक अतिरिक्त स्तर में उतरना पड़े, उदाहरण के लिए, उपयोगकर्ताओं के लिए संगठन:

              1query usersWithOrganizations {2  users {3    organizations {4      # this is a UserOrganization entity5      organization {6        name7      }8    }9  }10}

              This more elaborate way of storing many-to-many relationships will result in less data stored for the Subgraph, and therefore to a Subgraph that is often dramatically faster to index and to query.

              स्कीमा में टिप्पणियां जोड़ना

              As per GraphQL spec, comments can be added above schema entity attributes using the hash symbol #. This is illustrated in the example below:

              1type MyFirstEntity @entity {2  # unique identifier and primary key of the entity3  id: Bytes!4  address: Bytes!5}

              फुलटेक्स्ट सर्च फील्ड्स को परिभाषित करना

              पूर्ण पाठ खोज क्वेरी फ़िल्टर करती है और पाठ खोज इनपुट के आधार पर संस्थाओं को रैंक करती है। अनुक्रमित टेक्स्ट डेटा से तुलना करने से पहले क्वेरी टेक्स्ट इनपुट को तने में संसाधित करके पूर्ण टेक्स्ट क्वेरी समान शब्दों के लिए मैच वापस करने में सक्षम हैं।

              एक पूर्ण पाठ क्वेरी परिभाषा में क्वेरी का नाम, पाठ फ़ील्ड को संसाधित करने के लिए उपयोग किया जाने वाला भाषा शब्दकोश, परिणामों को क्रमबद्ध करने के लिए उपयोग किया जाने वाला रैंकिंग एल्गोरिदम और खोज में शामिल फ़ील्ड शामिल हैं। प्रत्येक पूर्ण-पाठ क्वेरी में कई फ़ील्ड शामिल हो सकते हैं, लेकिन सभी शामिल फ़ील्ड एक इकाई प्रकार से होने चाहिए।

              To add a fulltext query, include a _Schema_ type with a fulltext directive in the GraphQL schema.

              1type _Schema_2  @fulltext(3    name: "bandSearch"4    language: en5    algorithm: rank6    include: [{ entity: "Band", fields: [{ name: "name" }, { name: "description" }, { name: "bio" }] }]7  )89type Band @entity {10  id: Bytes!11  name: String!12  description: String!13  bio: String14  wallet: Address15  labels: [Label!]!16  discography: [Album!]!17  members: [Musician!]!18}

              The example bandSearch field can be used in queries to filter Band entities based on the text documents in the name, description, and bio fields. Jump to GraphQL API - Queries for a description of the fulltext search API and more example usage.

              1query {2  bandSearch(text: "breaks & electro & detroit") {3    id4    name5    description6    wallet7  }8}

              Feature Management: From specVersion 0.0.4 and onwards, fullTextSearch must be declared under the features section in the Subgraph manifest.

              भाषाओं का समर्थन किया

              एक अलग भाषा का चयन एक निश्चित, हालांकि कभी-कभी सूक्ष्म, पूर्ण पाठ खोज एपीआई पर प्रभाव डालेगा। पूर्ण-पाठ क्वेरी फ़ील्ड द्वारा कवर किए गए फ़ील्ड को चुनी हुई भाषा के संदर्भ में जांचा जाता है, इसलिए विश्लेषण और खोज क्वेरी द्वारा उत्पन्न शब्दांश भाषा से भाषा में भिन्न होते हैं। उदाहरण के लिए: समर्थित तुर्की शब्दकोश “टोकन” का उपयोग करते समय “टोकन” के लिए स्टेम किया जाता है, जबकि निश्चित रूप से, अंग्रेजी शब्दकोश इसे “टोकन” के लिए स्टेम करेगा।

              समर्थित भाषा शब्दकोश:

              Codeशब्दकोष
              simpleGeneral
              daDanish
              nlDutch
              enEnglish
              fiFinnish
              frFrench
              deGerman
              huHungarian
              itItalian
              noNorwegian
              ptपुर्तगाली
              roRomanian
              ruRussian
              esSpanish
              svSwedish
              trTurkish

              रैंकिंग एल्गोरिदम

              परिणाम ऑर्डर करने के लिए समर्थित एल्गोरिदम:

              AlgorithmDescription
              rankपरिणामों को व्यवस्थित करने के लिए पूर्ण पाठ क्वेरी की मिलान गुणवत्ता (0-1) का उपयोग करें।
              proximityRankSimilar to rank but also includes the proximity of the matches.
              ⁠GitHub पर संपादित करें⁠

              Subgraph ManifestWriting AssemblyScript Mappings
              इस पृष्ठ पर
              • Overview
              • संपत्ति परिभाषित करना
              • बिल्ट-इन स्केलर प्रकार
              • Enums
              • निकाय संबंध
              • रिवर्स लुकअप
              • स्कीमा में टिप्पणियां जोड़ना
              • फुलटेक्स्ट सर्च फील्ड्स को परिभाषित करना
              • भाषाओं का समर्थन किया
              • रैंकिंग एल्गोरिदम
              The GraphStatusTestnetBrand AssetsForumSecurityPrivacy PolicyTerms of Service