Usage of Library Fragment In RAML

 

In this tutorial we will demonstrate the usage of library fragment in raml. Library is one of the Raml Fragments. Main advantage of the raml Library is that is can defined multiple types and that can be referred from RAML file

To create Raml Library, login in to Anypoint platform and click on design center and click on Create Fragments

 

Usage of Library Fragment In RAML

 

Provide the name for new fragment

 

 

Update the RAML with below information

employee-library

Usage of Library Fragment In RAML
 

#%RAML 1.0 Library
usage:
types: 
    EmployeePersonalInfo: !include employee-personalinfo-dataType.raml
    EmployeeAddressInfo: !include employee-addressinfo-dataType.raml

 
Define the Individual DataType which is included as part of library

 

employee-addressinfo-dataType.raml

#%RAML 1.0 DataType
type: object
displayName: Address Details
properties:
  LocalAddress:
    type: object
    required: true
    displayName: "Local Address"
    description: "Local Address"
    properties:   
        Address_Line_1:
          type: string
          required: true
          displayName: "Address_Line_1"
          description: "Address_Line_1"
          example: "Flat 101"
        Address_Line_2:
          type: string
          required: true
          displayName: "Address_Line_2"
          description: "Address_Line_2"
          example: "Block-c"
        Area:
          type: string
          required: true
          displayName: "Area"
          description: "Area"
          example: "MG ROAD"   
        City:
          type: string
          required: true
          displayName: "City"
          description: "City"
          example: "Delhi"    
        Country:
          type: string
          required: true
          displayName: "Country"
          description: "Country"
          example: "India"
        PostalCode:
          type: string
          required: false
          displayName: "PostalCode"
          description: "PostalCode"
          example: "A09172"

 

employee-personalinfo-dataType.raml

 

#%RAML 1.0 DataType
type: object                    
displayName: Personal Details
properties:
  PersonalDetail:
    type: object
    required: true
    displayName: "PersonalDetail"
    description: "PersonalDetail"
    properties:   
        FirstName:
          type: string
          required: true
          displayName: "FirstName"
          description: "FirstName"
          example: "Mule"
        LastName:
          type: string
          required: true
          displayName: "LastName"
          description: "LastName"
          example: "Mulesy"
        Email:
          type: string
          required: true
          displayName: "Email"
          description: "Email"
          example: "[email protected]"   
        Phone:
          type: string
          required: true
          displayName: "Phone"
          description: "Phone"
          example: "01-793274093"    
        BloodGroup:
          type: string
          required: true
          displayName: "BloodGroup"
          description: "BloodGroup"
          example: "A+"

 

Once you create the Library as a fragment you can publish it on exchange to make it reusable across organization, else you can also use this inside individual raml specification in that case you can first create a RAML specification and add the file as a library or any other fragment

Create a RAML file In design Center to use the library created above, here we will use the library fragments which is published on exchange, please visit include fragments in raml for more details

See below how to include the library inside raml file, we use uses: to include the library and also refer this from exchange, in case of library it is not required to use !include to include the library

 

Usage of Library Fragment In RAML
 

#%RAML 1.0
baseUri: https://www.mulesoft.com/resources/ # 
title: employee
description: This API is to retrieve employeeid
version: v1
mediaType:  [ application/json ]
protocols:
  - HTTP
uses:
  mylib: /exchange_modules/bc3a8427-d13f-46b4-a2e1-e94c83fd72e0/employee-library/1.0.1/employee-library.raml
/employeepersonalinfo/{empId}:
  get:
    displayName: Retrieve employee personal details
    description: Retrieve employee personal details by empid
    responses:
      200:
        description: Success repsonse of employee personal details
        body:
          application/json:
            type: mylib.EmployeePersonalInfo

/employeeaddressinfo/{empId}:
  get:
    displayName: Retrieve employee address details
    description: Retrieve employee address details by empid
    responses:
      200:
        description: Success repsonse of employee address details
        body:
          application/json:
            type: mylib.EmployeeAddressInfo 

           

 

Sample application: Employee raml specification

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Gopi
Gopi
3 years ago

Good Example for beginners to practice. Please Provide number of POC for Best Practice